C# winform中修改comboBox内容值的高度

C# winform 中修改comboBox内容值的高度

    • 我的绑定方式
    • 若出现数据类型
    • 故我显示值的方式
    • 完整代码如下:

我的绑定方式

ArrayList mylist = new ArrayList();
mylist.Add(new DictionaryEntry(“0”, “请选择”));

cb.ValueMember = “Key”;
cb.DisplayMember = “Value”;
cb.DataSource = mylist;

若出现数据类型

说明绑定的是DictionaryEntry 而不是DictionaryEntry的value
C# winform中修改comboBox内容值的高度_第1张图片

故我显示值的方式

((DictionaryEntry)list.Items[e.Index]).Value.ToString()

完整代码如下:

  /// 
        /// 修改comboBox中值高度
        /// 
        /// 
        /// 
        public static void cmbBind(ComboBox list, int itemHeight)
        {
            list.DropDownStyle = ComboBoxStyle.DropDownList;
            list.ItemHeight = itemHeight;
            list.DrawMode = DrawMode.OwnerDrawFixed;

            list.DrawItem += new DrawItemEventHandler(delegate (object sender, DrawItemEventArgs e)
            {
                if (e.Index < 0)
                {
                    return;
                }
                e.DrawBackground();
                e.DrawFocusRectangle();
                e.Graphics.DrawString(((DictionaryEntry)list.Items[e.Index]).Value.ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds.X, e.Bounds.Y + 3);
            });
        }
   
   


   
//调用方式  
  cmbBind(this.cobroomCode, 20);



你可能感兴趣的:(Winform,c#,后端,c#)