zoukankan      html  css  js  c++  java
  • 如何让ComboBox的下拉列表宽度自适应内容的宽度

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

    如果能够让下拉列表的宽度随着内容的变化而变化,这个问题不就解决了。下面我们看看如何让ComboBox的下拉列表宽度自适应内容的宽度:
     1private 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,实现一个自定义的控件,在用户每次打开下拉列表的时候,让控件自动调整下拉列表的宽度。
     1using System;
     2using System.Collections.Generic;
     3using System.ComponentModel;
     4using System.Drawing;
     5using System.Data;
     6using System.Text;
     7using System.Windows.Forms;
     8
     9namespace 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}

  • 相关阅读:
    蒙特卡罗算法之素数测试
    蒙特卡罗算法之主元素问题
    拉斯维加斯随机化算法求解整数因子分解
    拉斯维加斯算法之n后问题
    舍伍德算法之跳跃表问题
    舍伍德算法之线性时间选择问题
    X oracle 11g 旧的归档日志无法清理
    问题 C: B C++时间类的运算符重载
    设有三个进程A、B、C,其中A与B构成一对生产者与消费者(A为生产者,B为消费者),共享一个由n个缓冲块组成的缓冲池;B与C也构成一对生产者与消费者(此时B为生产者,C为消费者)共享另一个由m个缓冲块组成的缓冲池。用P、V操作描述它们之间的同步关系。
    某寺庙,有小和尚、老和尚若干。有一水缸,由小和尚用水桶从井中提水入缸,老和尚用水桶从缸里取水饮用。水缸可容10桶水,水取自同一井中。水井径窄,每次只能容一个水桶取水。水桶总数为3个。每次入、取缸水仅为1桶,且不可以同时进行。试用P、V操作给出小和尚、老和尚动作的算法描述。
  • 原文地址:https://www.cnblogs.com/EthanCai/p/1237025.html
Copyright © 2011-2022 走看看