日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

C# 学习笔记(16)ComboBox下拉列表框宽度自适应

發布時間:2025/4/16 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# 学习笔记(16)ComboBox下拉列表框宽度自适应 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

C# 學習筆記(16)ComboBox下拉列表框寬度自適應

當下拉列表框中內容寬度大于下拉列表框寬度時

下拉列表框不能將內容全部顯示出來

可以在下拉時,對下拉列表框內容進行重繪

/// <summary>/// 列表項下拉窗口寬度自適應/// </summary>/// <param name="comboBox"></param>private void AdjustComboBoxDropDownListWidth(object comboBox){Graphics g = null;Font font = null;try{ComboBox senderComboBox = null;if (comboBox is ComboBox)senderComboBox = (ComboBox)comboBox;else if (comboBox is ToolStripComboBox)senderComboBox = ((ToolStripComboBox)comboBox).ComboBox;elsereturn;int width = senderComboBox.Width;g = senderComboBox.CreateGraphics();font = senderComboBox.Font;//checks if a scrollbar will be displayed.//If yes, then get its width to adjust the size of the drop down list.int vertScrollBarWidth =(senderComboBox.Items.Count > senderComboBox.MaxDropDownItems)? SystemInformation.VerticalScrollBarWidth : 0;int newWidth;foreach (object s in senderComboBox.Items) //Loop through list items and check size of each items.{if (s != null){newWidth = (int)g.MeasureString(s.ToString().Trim(), font).Width+ vertScrollBarWidth;if (width < newWidth)width = newWidth; //set the width of the drop down list to the width of the largest item.}}senderComboBox.DropDownWidth = width;}catch{ }finally{if (g != null)g.Dispose();}}private void comboBox1_DropDown(object sender, EventArgs e){AdjustComboBoxDropDownListWidth(comboBox1);}

總結

以上是生活随笔為你收集整理的C# 学习笔记(16)ComboBox下拉列表框宽度自适应的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。