电脑教程中文网
首页  动态网站建设学习 程序  笑话  论坛 娱乐  交友 ADSL  峄城  成功者
中文名:电脑教程中文网,收集了大量的电脑教程! 编程技术文档 游戏开发 笑话站暂时关闭 设为首页
网页设计 HTML | Dreamweaver | CSS | Firework | FrontPage WEB开发 ASP | JSP | PHP | .NET | CGI | JS | VBS | XML | IIS6 | Apache | PWS
程序设计 Java | C++ |VC++ | C# | Delphi | VB | C语言 | 汇编 | Pascal | Perl 数据库 MSSQL | MySQL | Access | VF | Oracle | DB2 | SYBASE |
办公软件 Word | Excel | WPS | PowerPoint 动画平面 Photoshop | ACDSee | 3Dmax | Flash | Coreldraw |
操作系统 Windows 2000 | Windows XP | Windows 2003 | SCO Unix | Windows Vista | unix、Linux | 综合| 服务器 | 系统安全| 黑客技术
其  他 UltraDev | DOS | UML | PWS | Powerbuilder | 开发心得 | 设计理念 | 病毒库 | 其他 | LightTPD (分类排序给您带来不便请谅解)
推  荐: Java文档500篇》《ASP.NET与相关数据库技术高级指南》《TC图形函数详解》《C函数速查手册》《C语言编程宝典之一》《MFC深入浅出》《黑客零起点》《VC++ 编程指南》《JScript 用户指南》 《CSS教程宝典》《Microsoft Jet SQL 参考》《delphi技巧集合》《MySQL 4.1.0 中文参考手册》《MySQL中文手册
【导航】 您现在的位置 : 首页 - .NET教程 - 《.NET技术文档专区》- 如何让comboxBox的下拉列表宽度自适应内容的宽度

如何让comboxBox的下拉列表宽度自适应内容的宽度

日期:2008-1-28 15:58:00 作者:土成木易 人气:     来源:CSDN




在winform编程中,combox是我们经常用到的控件,往往因为界面排版或者其它原因,comboBox的宽度受到限制,而下拉列表中的内容太长。如果按照combobox的默认设置 ,下拉列表和comboBox的宽度一样,并不会跟随内容的变化而变化,这就造成下拉列表中有些项的内容太长而不能全部显示出来,就是下面这个样子:


如果能够让下拉列表的宽度随着内容的变化而变化,这个问题不就解决了。下面我们看看如何让comboxBox的下拉列表宽度自适应内容的宽度:
private void AdjustComboBoxDropDownListWidth(object comboBox)
        
{
            Graphics g 
= null;
            Font font 
= null;
            
try
            
{
                ComboBox senderComboBox 
= null;
                
if (comboBox is ComboBox)
                    senderComboBox 
= (ComboBox)comboBox;
                
else if (comboBox is ToolStripComboBox)
                    senderComboBox 
= ((ToolStripComboBox)comboBox).ComboBox;
                
else
                    
return;

                
int width = senderComboBox.Width;
                g 
= senderComboBox.CreateGraphics();
                font 
= senderComboBox.Font;

                
//checks if a scrollbar will be displayed.
                
//If yes, then get its width to adjust the size of the drop down list.
                int vertScrollBarWidth =
                    (senderComboBox.Items.Count 
> senderComboBox.MaxDropDownItems)
                    
? SystemInformation.VerticalScrollBarWidth : 0;

                
int newWidth;
                
foreach (object s in senderComboBox.Items)  //Loop through list items and check size of each items.
                {
                    
if (s != null)
                    
{
                        newWidth 
= (int)g.MeasureString(s.ToString().Trim(), font).Width
                            
+ vertScrollBarWidth;
                        
if (width < newWidth)
                            width 
= newWidth;   //set the width of the drop down list to the width of the largest item.
                    }

                }

                senderComboBox.DropDownWidth 
= width;
            }

            
catch
            
{ }
            
finally
            
{
                
if (g != null)
                    g.Dispose();
            }

        }

上面的方法,只要在我们向combox添加完所有项后,调用一下,就可以调整comboBox下拉列表的宽度了
private void button1_Click(object sender, EventArgs e)
        
{
            comboBox1.Items.Add(
"美国");
            comboBox1.Items.Add(
"中华人民共和国");
            comboBox1.Items.Add(
"新疆维吾尔族自治区");
            AdjustComboBoxDropDownListWidth(comboBox1);
        }

到这里,问题并没有完结,如果每次在我们向comboBox中添加一项后,就要调用一下这个方法,那就太麻烦了。能不能把这种自适应宽度的功能集成到comboBox中呢?可以,这里我们继承ComboBox,实现一个自定义的控件,在用户每次打开下拉列表的时候,让控件自动调整下拉列表的宽度。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication2
{
    
class MyComboBox : ComboBox
    
{
        
protected override void OnDropDown(EventArgs e)
        
{
            
base.OnDropDown(e);
            AdjustComboBoxDropDownListWidth();  
//调整comboBox的下拉列表的大小
        }


        
private void AdjustComboBoxDropDownListWidth()
        
{
            Graphics g 
= null;
            Font font 
= null;
            
try
            
{
                
int width = this.Width;
                g 
= this.CreateGraphics();
                font 
= this.Font;

                
//checks if a scrollbar will be displayed.
                
//If yes, then get its width to adjust the size of the drop down list.
                int vertScrollBarWidth =
                    (
this.Items.Count > this.MaxDropDownItems)
                    
? SystemInformation.VerticalScrollBarWidth : 0;

                
int newWidth;
                
foreach (object s in this.Items)  //Loop through list items and check size of each items.
                {
                    
if (s != null)
                    
{
                        newWidth 
= (int)g.MeasureString(s.ToString().Trim(), font).Width
                            
+ vertScrollBarWidth;
                        
if (width < newWidth)
                            width 
= newWidth;   //set the width of the drop down list to the width of the largest item.
                    }

                }

                
this.DropDownWidth = width;
            }

            
catch
            
{ }
            
finally
            
{
                
if (g != null)
                    g.Dispose();
            }

        }

    }

}





www.CLDE.net - CLDE电脑教程中文网
转自CLDE.NET


本文由CLDE.NET原创或整理(收集),如您需转载,请保留一下链接,谢谢!


下一篇:号称最稳定 Linux内核2.6.24版本发布
※视频教程※
ASP入门到精通视频教程
西安电科大JSP视频教程
photoshop cs2视频教程
flash动画设计师视频教程
Dreamweaver视频教程
VB从入门到精通视频教程
 
 


网站首页 - 友情链接 - 公司简介 - 联系方式 - 广告投放 - 客户服务 - 错误报告 - 免责声明 - About us
CLDE.NET电脑教程中文网版权所有 未经许可禁止镜象和复制本站资料 MSN:CLDE_NET@hotmail.com
技术支持:CLDE.NET信息中心 鲁ICP备05039940号 友情链接QQ:784079(隐)