combobox下拉列表的内容长度问题

ningyong58 2008-08-25 04:35:43
在MshFlexg中嵌套ComboBox的存在下拉列表,存在长度问题.请问各位大侠有何高招.


http://www.codeproject.com/KB/combobox/ComboBoxAutoWidth.aspx中,用VC写了如下代码
Adjust combo box drop down list width to longest string width
Introduction
This small snippet of code will show you how to automatically adjust the size of the drop down list of a combo box to fit the size of the longest string in its items.

Code
Step 1: Add an event handler for the DropDown event of the combo box. Call it AdjustWidthComboBox_DropDown for the sake of the following code.

Step 2: Add the following event handler code.

private void AdjustWidthComboBox_DropDown(object sender, System.EventArgs e)
{
ComboBox senderComboBox = (ComboBox)sender;
int width = senderComboBox.DropDownWidth;
Graphics g = senderComboBox.CreateGraphics();
Font font = senderComboBox.Font;
int vertScrollBarWidth =
(senderComboBox.Items.Count>senderComboBox.MaxDropDownItems)
?SystemInformation.VerticalScrollBarWidth:0;

int newWidth;
foreach (string s in ((ComboBox)sender).Items)
{
newWidth = (int) g.MeasureString(s, font).Width
+ vertScrollBarWidth;
if (width < newWidth )
{
width = newWidth;
}
}
senderComboBox.DropDownWidth = width;
}
Explanation
请问各位大侠,还有其它的方法吗?
...全文
237 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ningyong58 2008-08-27
  • 打赏
  • 举报
回复
不结帖,每次弹出一大堆东西,没人回答问题又结不了帖,只好如此了。谢谢你来JF。
yachong 2008-08-26
  • 打赏
  • 举报
回复
为啥每次都是我JF呢
搞得跟倒分似的
呵呵
yachong 2008-08-26
  • 打赏
  • 举报
回复
JF
ningyong58 2008-08-26
  • 打赏
  • 举报
回复
JF-----JT

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧