/// <summary>/// 列表項(xiàng)下拉窗口寬度自適應(yīng)/// </summary>/// <param name="comboBox"></param>privatevoidAdjustComboBoxDropDownListWidth(object comboBox){Graphics g =null;Font font =null;try{ComboBox senderComboBox =null;if(comboBox isComboBox)senderComboBox =(ComboBox)comboBox;elseif(comboBox isToolStripComboBox)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();}}privatevoidcomboBox1_DropDown(object sender,EventArgs e){AdjustComboBoxDropDownListWidth(comboBox1);}