winform窗体中 ComboBox 控件的DropDownWidth属性根据显示的文本设置下拉列表的显示宽度

 1 private void BindCombobox()
 2         {
 3             if (!GetConn.OpenConn())
 4             {
 5                 return;
 6             }
 7             cboRestaurant.DataSource = DCLibrary.Current.RestaurantList;
 8             cboRestaurant.ValueMember = "systemkey";
 9             cboRestaurant.DisplayMember = "Description2";
10             Grap();
11            
12         }
13         /// <summary>
14         /// 根据combobox 显示的文本动态设置【DropDownWidth】属性
15         /// </summary>
16         private void Grap() {
17             System.Drawing.Graphics g = cboRestaurant.CreateGraphics();
18             float width = 0;
19             foreach (DCLibrary.Model.RestaurantInfo s in cboRestaurant.Items)
20             {
21                 if (width < g.MeasureString(s.Description2, cboRestaurant.Font).Width)
22                     width = g.MeasureString(s.Description2, cboRestaurant.Font).Width;
23             }
24             if (width > cboRestaurant.DropDownWidth)
25                 cboRestaurant.DropDownWidth = Convert.ToInt32(width);
26             g.Dispose(); 
27         }
posted @ 2012-10-07 15:23  DuFeng  阅读(1489)  评论(0编辑  收藏  举报