和谐拯救软件
博客园
社区
首页
新随笔
联系
管理
订阅
随笔- 15 文章- 0 评论- 25
如何让ComboBox的下拉列表宽度自适应内容的宽度
在Win Form编程中,ComboBox是我们经常用到的控件,往往因为界面排版或者其它原因,ComboBox的宽度受到限制,而下拉列表中的内容太长。 如果按照
ComboBox
的默认设置 ,下拉列表和
ComboBox
的宽度一样,并不会跟随内容的变化而变化,这就造成下拉列表中有些项的内容太长而不能全部显示出来,就是下面这个样子:
如果能够让下拉列表的宽度随着内容的变化而变化,这个问题不就解决了。下面我们看看如何让
ComboBox
的下拉列表宽度自适应内容的宽度:
1
private
void
AdjustComboBoxDropDownListWidth(
object
comboBox)
2
{
3
Graphics g
=
null
;
4
Font font
=
null
;
5
try
6
{
7
ComboBox senderComboBox
=
null
;
8
if
(comboBox
is
ComboBox)
9
senderComboBox
=
(ComboBox)comboBox;
10
else
if
(comboBox
is
ToolStripComboBox)
11
senderComboBox
=
((ToolStripComboBox)comboBox).ComboBox;
12
else
13
return
;
14
15
int
width
=
senderComboBox.Width;
16
g
=
senderComboBox.CreateGraphics();
17
font
=
senderComboBox.Font;
18
19
//
checks if a scrollbar will be displayed.
20
//
If yes, then get its width to adjust the size of the drop down list.
21
int
vertScrollBarWidth
=
22
(senderComboBox.Items.Count
>
senderComboBox.MaxDropDownItems)
23
?
SystemInformation.VerticalScrollBarWidth :
0
;
24
25
int
newWidth;
26
foreach
(
object
s
in
senderComboBox.Items)
//
Loop through list items and check size of each items.
27
{
28
if
(s
!=
null
)
29
{
30
newWidth
=
(
int
)g.MeasureString(s.ToString().Trim(), font).Width
31
+
vertScrollBarWidth;
32
if
(width
<
newWidth)
33
width
=
newWidth;
//
set the width of the drop down list to the width of the largest item.
34
}
35
}
36
senderComboBox.DropDownWidth
=
width;
37
}
38
catch
39
{ }
40
finally
41
{
42
if
(g
!=
null
)
43
g.Dispose();
44
}
45
}
如果每次在我们向ComboBox中添加一项后,就要调用一下这个方法,那就太麻烦了。能不能把这种自适应宽度的功能集成到 ComboBox中呢?这里我们继承ComboBox,实现一个自定义的控件,在用户每次打开下拉列表的时候,让控件自动调整下拉列表的宽度。
1
using
System;
2
using
System.Collections.Generic;
3
using
System.ComponentModel;
4
using
System.Drawing;
5
using
System.Data;
6
using
System.Text;
7
using
System.Windows.Forms;
8
9
namespace
WindowsApplication2
10
{
11
class
MyComboBox : ComboBox
12
{
13
protected
override
void
OnDropDown(EventArgs e)
14
{
15
base
.OnDropDown(e);
16
AdjustComboBoxDropDownListWidth();
//
调整comboBox的下拉列表的大小
17
}
18
19
private
void
AdjustComboBoxDropDownListWidth()
20
{
21
Graphics g
=
null
;
22
Font font
=
null
;
23
try
24
{
25
int
width
=
this
.Width;
26
g
=
this
.CreateGraphics();
27
font
=
this
.Font;
28
29
//
checks if a scrollbar will be displayed.
30
//
If yes, then get its width to adjust the size of the drop down list.
31
int
vertScrollBarWidth
=
32
(
this
.Items.Count
>
this
.MaxDropDownItems)
33
?
SystemInformation.VerticalScrollBarWidth :
0
;
34
35
int
newWidth;
36
foreach
(
object
s
in
this
.Items)
//
Loop through list items and check size of each items.
37
{
38
if
(s
!=
null
)
39
{
40
newWidth
=
(
int
)g.MeasureString(s.ToString().Trim(), font).Width
41
+
vertScrollBarWidth;
42
if
(width
<
newWidth)
43
width
=
newWidth;
//
set the width of the drop down list to the width of the largest item.
44
}
45
}
46
this
.DropDownWidth
=
width;
47
}
48
catch
49
{ }
50
finally
51
{
52
if
(g
!=
null
)
53
g.Dispose();
54
}
55
}
56
}
57
}
posted @ 2008-07-06 21:30
Baozi
阅读(311)
评论(2)
编辑
收藏
网摘
所属分类:
开发技术
发表评论
回复
引用
查看
#1楼
2008-07-07 09:17 |
cdboy
收藏
回复
引用
#2楼
2008-12-02 20:54 |
Jht Huang [未注册用户]
不错
新用户注册
刷新评论列表
标题
姓名
主页
Email
(博主才能看到)
验证码
*
看不清,换一张
[
登录
][
注册
]
内容(请不要发表任何与政治相关的内容)
网站首页
新闻频道
社区
小组
博问
网摘
人才
找找看
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
Google站内搜索
China-pub 计算机图书网上专卖店!6.5万品种 2-8折!
近千种 9-95 新二手计算图书火热销售中!
开发者征途系统新作:《设计模式——基于C#的工程化实现及扩展》
相关文章:
相关链接:
所属分类的其他文章:
C#中的函数委托
如何让ComboBox的下拉列表宽度自适应内容的宽度
避免对C#中float,double,decimal的错误理解
C#中“覆盖”和“隐藏”的区别
最新IT新闻:
谷歌地图中国版开始显示Panoramio地理标记图片
谷歌李开复:我的传奇人生源于十句箴言
Twitter杀手Pownce被收购 两周内关闭服务
Live Search新增“即时返现”服务
MySpace CEO:明年广告营收仍会增长
<
2008年7月
>
日
一
二
三
四
五
六
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
与我联系
发短消息
搜索
常用链接
我的随笔
我的空间
我的短信
我的评论
更多链接
我的参与
我的新闻
最新评论
我的标签
留言簿
给我留言
查看留言
我的标签
Windows via C/C++(1)
覆盖(1)
隐藏(1)
Anders Hejlsberg(1)
decimal(1)
Flex(1)
Flash(1)
Rich experiences(1)
jqs(1)
jre(1)
随笔分类
开发技术(4)
(rss)
其它(10)
(rss)
数据库
(rss)
系统维护(1)
(rss)
随笔档案
2008年12月 (1)
2008年11月 (4)
2008年8月 (2)
2008年7月 (8)
未分类
De Dream’ 交互设计
HYXT Blog
JavaScript Kit
最新评论
1. re: 很吸引眼球的Windows Live产品线
兄弟,你把这东西放到首页上,是不是作娱记的阿? (娱记)
2. re: 很吸引眼球的Windows Live产品线
msn家族产品是微软所有产品中最垃圾的!!!!1 (w3c)
3. re: 很吸引眼球的Windows Live产品线
@杨芹勍
呵呵,谢谢啊 (Baozi)
4. re: 很吸引眼球的Windows Live产品线
兄弟,就凭为了发博客那么晚都不休息,我也得顶一个,沙发~~ (杨芹勍)
5. re: 如何让ComboBox的下拉列表宽度自适应内容的宽度
不错 (Jht Huang)
阅读排行榜
1. Firefox的刷新功能与Safari,IE的差距(1033)
2. 避免对C#中float,double,decimal的错误理解(738)
3. Windows 2003 标准版_企业版_SP1_SP2_R2的区别(467)
4. 如何让ComboBox的下拉列表宽度自适应内容的宽度(311)
5. 《Windows via C/C++》简介(245)
评论排行榜
1. Firefox的刷新功能与Safari,IE的差距(13)
2. 很吸引眼球的Windows Live产品线(4)
3. 《Windows via C/C++》简介(3)
4. 避免对C#中float,double,decimal的错误理解(3)
5. 如何让ComboBox的下拉列表宽度自适应内容的宽度(2)