C#学习笔记(16 )如果ComboBox下拉列表框的宽度自适应下拉列表框中的内容宽度大于下拉列表框的宽度
不能在下拉列表框中显示所有内容
下拉列表框的内容可以在下拉列表框中重新绘制
///summary ///列表项目下拉窗口宽度自适应///summary//param name=' combobox '/paramprivatevoidadjustcomboboxdropdownlox try { comboboxsendercombobox=null; 组合盒体(if )发送器组合盒体=)组合盒体(combobox )组合盒体; else if (comboboxistoolstripcombobox ) sendercombobox=) toolstripcombobox ).combobox; else返回; int width=senderComboBox.Width; g=sender combobox.create graphics (; font=senderComboBox.Font; //checksifascrollbarwillbedisplayed.//if yes, thengetitswidthtoadjustthesizeofthedropdownlist.intvertscrollbarwidth=(sender combobox.items.countsendercombobox.max 新建窗口; foreach (objectsinsendercombobox.items )/loopthroughlistitemsandchecksizeofeachitems.{ if (s!=null () newwidth=(int ) g.measurestring (s.tostring ).Trim ),font ).Width vertScrollBarWidth; 新宽度(if )宽度=新宽度; //setthewidthofthedropdownlisttothewidthofthelargestitem.} sender combobox.drop down width=width; }catch{}finally{if(g!=null(g.dispose ); } privatevoidcombobox1_ drop down (对象发送器,事件e ) adjustcomboboxdropdrownlistwidth (combobox1); }