1. 程式人生 > >comboBox 下拉寬度自適應

comboBox 下拉寬度自適應

rap com count send mbo 寬度 eas obj table

///適用combobox綁定datatable

private void comboBox_DataSourceChanged(object sender, EventArgs e)
{
ComboBox cbb = sender as ComboBox;
string s = "";
foreach (DataRow row in (cbb.DataSource as DataTable).Rows)
{
string str = row[cbb.DisplayMember].ToString();
if (s.Length < str.Length)
{
s = str;
}
}
cbb.DropDownWidth = (int)cbb.CreateGraphics().MeasureString(s, cbb.Font).Width
+ (cbb.Items.Count > cbb.MaxDropDownItems ? SystemInformation.VerticalScrollBarWidth : 0);
}

comboBox 下拉寬度自適應