/// <summary>
/// ComboBox 自动调整组合框下拉部分的宽度
/// </summary>
void ResizeComBox(ComboBox cb,string rowName)
{
foreach (DataRowView dr in cb.Items)
{
cb.DropDownWidth = Math.Max(cb.DropDownWidth, TextRenderer.MeasureText(dr[rowName].ToString(), cb.Font).Width);
}
cb.ClientSize = new Size(cb.DropDownWidth, cb.ClientSize.Height);
}
使用方法
cbChannels.DataSource = DB.GetChannels();
cbChannels.ValueMember = "ChannelId";
cbChannels.DisplayMember = "ChannelName";
ResizeComBox(cbChannels, "ChannelName");