zoukankan      html  css  js  c++  java
  • 如何让组合框的宽度自动适应

    在Windows Forms应用程序开发过程中,我们经常会用到组合框(ComboBox),但它的那个下拉列表宽度是固定的,那么如何让它支持自动适应的宽度呢。下面是一个小例子

    using System;
    using System.Drawing;
    using System.Linq;
    using System.Windows.Forms;
    using System.IO;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    
                Load += new EventHandler(Form1_Load);
            }
    
            void Form1_Load(object sender, EventArgs e)
            {
    
                var dir = new DirectoryInfo("e:\\temp");
                var query = from f in dir.GetFiles()
                            select new
                            {
                                FullName = f.FullName,
                                Name = f.Name
                            };
    
                comboBox1.DataSource = query.ToArray();
                comboBox1.DisplayMember = "Name";
                comboBox1.ValueMember = "FullName";
    
    
                
                using (Graphics g = comboBox1.CreateGraphics())
                {
                    int widestWidth = comboBox1.DropDownWidth;
                    foreach (var item in query.Select(v=>v.Name))
                    {
                        int current = (int)g.MeasureString(item, comboBox1.Font).Width;
                        if (current > widestWidth) widestWidth = current;
                    }
                    comboBox1.DropDownWidth = widestWidth;
                }
    
            }
    
        }
    }
    
  • 相关阅读:
    算法之二叉树各种遍历
    File类基本操作之OutputStream字节输出流
    W3C DOM 事件模型(简述)
    Linux多线程编程小结
    linux下getsockopt和setsockopt具体解释及測试
    MyBatis入门学习(一)
    [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify uniq
    java中substring的使用方法
    Java Map遍历方式的选择
    E6全部刷机包
  • 原文地址:https://www.cnblogs.com/chenxizhang/p/1739590.html
Copyright © 2011-2022 走看看