小弟最近在寫WinForm,遇到ComboBox下拉選單時
如果下拉選項文字太長會被切掉的問題..如下圖所示:
去找了一下解決方法...分享給大家呀...
c#(WinForm)
private void Form1_Load(object sender, EventArgs e)
{
{
comboBox1.Items.Add("微軟Tech.Ed 2008研討會");
comboBox1.Items.Add("MIX08 Taipei");
comboBox1.Items.Add("MIX08 Taipei");
//ComboBox AutoSize
int maxSize = 0;
System.Drawing.Graphics g = CreateGraphics();
int maxSize = 0;
System.Drawing.Graphics g = CreateGraphics();
for (int i = 0; i < comboBox1.Items.Count; i++)
{
comboBox1.SelectedIndex = i;
SizeF size = g.MeasureString(comboBox1.Text, comboBox1.Font);
if (maxSize < (int)size.Width)
{
maxSize = (int)size.Width;
}
}
{
comboBox1.SelectedIndex = i;
SizeF size = g.MeasureString(comboBox1.Text, comboBox1.Font);
if (maxSize < (int)size.Width)
{
maxSize = (int)size.Width;
}
}
comboBox1.DropDownWidth = comboBox1.Width;
if (comboBox1.DropDownWidth < maxSize)
{
comboBox1.DropDownWidth = maxSize;
}
{
comboBox1.DropDownWidth = maxSize;
}
comboBox1.SelectedIndex = -1;
}
執行結果:
參考網址:
沒有留言:
張貼留言