zoukankan      html  css  js  c++  java
  • 将组合框下拉列表宽度调整为最长字符串宽度

    介绍 这段代码将向您展示如何自动调整组合框的下拉列表的大小,以适应其项中最长字符串的大小。 代码 步骤1:为组合框的下拉事件添加一个事件处理程序。为完成以下代码,将其称为AdjustWidthComboBox_DropDown。 步骤2:添加以下事件处理程序代码。隐藏,复制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;
    }

    解释 代码假设处理程序仅用于组合框,并执行强制转换,而不检查发送方的类型。 然后获取组合框的图形和字体对象,这将帮助我们测量列表中字符串的大小。 senderComboBox.Items.Count> senderComboBox。MaxDropDownItems检查是否显示滚动条。如果它要被显示,那么我们获得它的宽度来相应地调整下拉列表的大小。 然后滚动项目列表,检查每个项目的大小,最后将下拉列表的宽度设置为包括滚动条宽度在内的最大项目的宽度。 本文转载于:http://www.diyabc.com/frontweb/news291.html

  • 相关阅读:
    PG
    unzip
    yum
    PG
    SQL
    Grails
    Grails
    Grails
    Chrome
    HTML
  • 原文地址:https://www.cnblogs.com/Dincat/p/13437450.html
Copyright © 2011-2022 走看看