本文實(shí)例講述了C#實(shí)現(xiàn)讓ListBox適應(yīng)最大Item寬度的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
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#程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選