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

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

如果能够让下拉列表的宽度随着内容的变化而变化,这个问题不就解决了。下面我们看看如何让 ComboBox的下拉列表宽度自适应内容的宽度:
 1 None.gif private   void  AdjustComboBoxDropDownListWidth( object  comboBox)
 2 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 3InBlock.gif    Graphics g = null;
 4InBlock.gif    Font font = null;
 5InBlock.gif    try
 6ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 7InBlock.gif        ComboBox senderComboBox = null;
 8InBlock.gif        if (comboBox is ComboBox)
 9InBlock.gif            senderComboBox = (ComboBox)comboBox;
10InBlock.gif        else if (comboBox is ToolStripComboBox)
11InBlock.gif            senderComboBox = ((ToolStripComboBox)comboBox).ComboBox;
12InBlock.gif        else
13InBlock.gif            return;
14InBlock.gif
15InBlock.gif        int width = senderComboBox.Width;
16InBlock.gif        g = senderComboBox.CreateGraphics();
17InBlock.gif        font = senderComboBox.Font;
18InBlock.gif
19InBlock.gif        //checks if a scrollbar will be displayed.
20InBlock.gif        //If yes, then get its width to adjust the size of the drop down list.
21InBlock.gif        int vertScrollBarWidth =
22InBlock.gif            (senderComboBox.Items.Count > senderComboBox.MaxDropDownItems)
23InBlock.gif            ? SystemInformation.VerticalScrollBarWidth : 0;
24InBlock.gif
25InBlock.gif        int newWidth;
26InBlock.gif        foreach (object s in senderComboBox.Items)  //Loop through list items and check size of each items.
27ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
28InBlock.gif            if (s != null)
29ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
30InBlock.gif                newWidth = (int)g.MeasureString(s.ToString().Trim(), font).Width
31InBlock.gif                    + vertScrollBarWidth;
32InBlock.gif                if (width < newWidth)
33InBlock.gif                    width = newWidth;   //set the width of the drop down list to the width of the largest item.
34ExpandedSubBlockEnd.gif            }

35ExpandedSubBlockEnd.gif        }

36InBlock.gif        senderComboBox.DropDownWidth = width;
37ExpandedSubBlockEnd.gif    }

38InBlock.gif    catch
39ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{ }
40InBlock.gif    finally
41ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
42InBlock.gif        if (g != null)
43InBlock.gif            g.Dispose();
44ExpandedSubBlockEnd.gif    }

45ExpandedBlockEnd.gif}

如果每次在我们向ComboBox中添加一项后,就要调用一下这个方法,那就太麻烦了。能不能把这种自适应宽度的功能集成到 ComboBox中呢?这里我们继承ComboBox,实现一个自定义的控件,在用户每次打开下拉列表的时候,让控件自动调整下拉列表的宽度。
 1 None.gif using  System;
 2 None.gif using  System.Collections.Generic;
 3 None.gif using  System.ComponentModel;
 4 None.gif using  System.Drawing;
 5 None.gif using  System.Data;
 6 None.gif using  System.Text;
 7 None.gif using  System.Windows.Forms;
 8 None.gif
 9 None.gif namespace  WindowsApplication2
10 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
11InBlock.gif    class MyComboBox : ComboBox
12ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
13InBlock.gif        protected override void OnDropDown(EventArgs e)
14ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
15InBlock.gif            base.OnDropDown(e);
16InBlock.gif            AdjustComboBoxDropDownListWidth();  //调整comboBox的下拉列表的大小
17ExpandedSubBlockEnd.gif        }

18InBlock.gif
19InBlock.gif        private void AdjustComboBoxDropDownListWidth()
20ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
21InBlock.gif            Graphics g = null;
22InBlock.gif            Font font = null;
23InBlock.gif            try
24ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
25InBlock.gif                int width = this.Width;
26InBlock.gif                g = this.CreateGraphics();
27InBlock.gif                font = this.Font;
28InBlock.gif
29InBlock.gif                //checks if a scrollbar will be displayed.
30InBlock.gif                //If yes, then get its width to adjust the size of the drop down list.
31InBlock.gif                int vertScrollBarWidth =
32InBlock.gif                    (this.Items.Count > this.MaxDropDownItems)
33InBlock.gif                    ? SystemInformation.VerticalScrollBarWidth : 0;
34InBlock.gif
35InBlock.gif                int newWidth;
36InBlock.gif                foreach (object s in this.Items)  //Loop through list items and check size of each items.
37ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
38InBlock.gif                    if (s != null)
39ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
40InBlock.gif                        newWidth = (int)g.MeasureString(s.ToString().Trim(), font).Width
41InBlock.gif                            + vertScrollBarWidth;
42InBlock.gif                        if (width < newWidth)
43InBlock.gif                            width = newWidth;   //set the width of the drop down list to the width of the largest item.
44ExpandedSubBlockEnd.gif                    }

45ExpandedSubBlockEnd.gif                }

46InBlock.gif                this.DropDownWidth = width;
47ExpandedSubBlockEnd.gif            }

48InBlock.gif            catch
49ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{ }
50InBlock.gif            finally
51ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
52InBlock.gif                if (g != null)
53InBlock.gif                    g.Dispose();
54ExpandedSubBlockEnd.gif            }

55ExpandedSubBlockEnd.gif        }

56ExpandedSubBlockEnd.gif    }

57ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/EthanCai/archive/2008/07/06/1237025.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值