本文实例讲述了C#实现让ListBox适应最大Item宽度的方法。分享给大家供大家参考。具体实现方法如下:
private void button1_Click(object sender, EventArgs e){ for (int i = 1; i <= 16; i++) { listBox1.Items.Add("TTTTTTTTTTTTTTTTTTTTTTTTTTTTT" + i.ToString()); }}private void button2_Click(object sender, EventArgs e){ SizeF size = listBox1.CreateGraphics(). MeasureString(listBox1.Items[0].ToString(), listBox1.Font); MessageBox.Show(Convert.ToInt32(size.Width).ToString()); listBox1.Width = Convert.ToInt32(size.Width)+20;}
同理CheckedListBox也可以这样控制
希望本文所述对大家的C#程序设计有所帮助。
新闻热点
疑难解答