ComboBox 自动调整组合框下拉部分的宽度

/// <summary>
        /// ComboBox 自动调整组合框下拉部分的宽度
        /// </summary>
        void ResizeComBox(ComboBox cb,string rowName)
        {
            foreach (DataRowView dr in cb.Items)
            {
                cb.DropDownWidth = Math.Max(cb.DropDownWidth, TextRenderer.MeasureText(dr[rowName].ToString(), cb.Font).Width);
            }
            cb.ClientSize = new Size(cb.DropDownWidth, cb.ClientSize.Height);
        }

使用方法

cbChannels.DataSource = DB.GetChannels();
            cbChannels.ValueMember = "ChannelId";
            cbChannels.DisplayMember = "ChannelName";
            ResizeComBox(cbChannels, "ChannelName");

上一篇:SharePoint 2013 图文开发系列之代码定义列表


下一篇:(6) MySQL慢查询日志的使用