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");

posted @ 2013-09-24 14:45  simadi  阅读(283)  评论(0编辑  收藏  举报