comboBox 下拉宽度自适应

本文介绍了一种在Windows窗体中使用C#实现Combobox与Datatable绑定时,自动调整Combobox下拉宽度的方法,确保下拉菜单中的最长项能够完全显示,避免文字截断。

///适用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);
}

转载于:https://www.cnblogs.com/masiteyi/p/9955980.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值