我有一個帶有TabControl和ListView的Windows表單。
當我運行應用程式時,我想讓TabControl的Width增加/減少,以顯示所有的TabPages而不需要水平滾動條,并且讓Form相應地調整它的Width,以確保TabControl和ListView是可見的。
下面是一個螢屏截圖。
uj5u.com熱心網友回復:
為了使TabControl的自動大小與它的標題的大小一致,你需要計算每個標題的文本寬度。這在
從該表單開始,我將在運行時添加8個標簽,計算標簽中文本的寬度 填充尺寸x2(標簽的兩側),然后根據需要調整控制元件的大小。
public Form1()
{
InitializeComponent()。
//Clear our default tabs.
tabControl1.TabPages.Clear()。
//Add more tabs than would be visible by default
for (int i=1; i<=8; i )
{
tabControl1.TabPages.Add("Tab" i.ToString())。
}
調整TabControl()的大小。
調整ListViewControl()的大小。
調整表格大小()。
}
void ResizeTabControl()
{
int tabCount = tabControl1.TabCount。
float length = 0;
using (Graphics g = CreateGraphics())
{
//Iterate through the tabs and get the length of the text.。
for (int i = 0; i <= tabCount - 1; i )
length = g.MeasureString(tabControl1.TabPages[i].Text, tabControl1.Font).Width;
}
/Resize the tab control where X is the length of all text in the tabs plus padding x 2 x total tabs.
tabControl1.Size = new Size(Convert.ToInt32(length) (tabCount * 2 * tabControl1.Padding.X), tabControl1.Width) 。
}
void ResizeListViewControl()
{
//Move listview 10 pixels away from tabcontrol's edge[/span]。
listView1.Location = new Point(tabControl1.Location.X tabControl1.Width 10, listView1.Location.Y) 。
}
void ResizeForm()
{
//Resize Form to accommodate changes.
this.Width = listView1.Location.X listView1.Width 20;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/326910.html
標籤:




