Combobox下拉框加深显示Combobox中text的值,取消原有的填充显示

在输入Combobox内的text的值后,点击下拉按钮,Combobox下拉框内的值的内容如果与text的值相同,则会加深显示,底色为蓝色。有时候,Combobox下拉框内容的值没有经过排序,在进行关联显示的时候,可能不会显示到我们想要的准确的值。比如说,Combobox的下拉框的内容如下

TestTestTest

TestTest

Test

TestTestTestTestTest

如果,在Combobox中输入Test,那么点击下拉框时,是“TestTestTest”这一栏加深显示,而不是我们所要的“Test”。

对于这种情况,以及我们想要修改默认的加深颜色和字体,可以采用一下的方法:

private void Form0109_Load(object sender, EventArgs e)
        {
            DefineComboBox ac = new DefineComboBox();
            ac.Location = new Point(100,30);
            var test3 = new List<string>();
            test3.Add("TestTestTest");
            test3.Add("TestTestTestTestTest");
            test3.Add("Test");
            test3.Add("TestTest");
            test3.Add("TestTestTestTestTestTestTestTestTestTestTestTestTestTestTest");
            ac.DataSource = test3;
            this.Controls.Add(ac);
        }
        class DefineComboBox : ComboBox
        {
            new public System.Windows.Forms.DrawMode DrawMode { get; set; }
            public Color HighlightColor { get; set; }//define the highlight color
            public DefineComboBox()
            {
                base.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
                this.HighlightColor = Color.LightBlue;
                this.DrawItem += new DrawItemEventHandler(DefineComboBox_DrawItem);
            }
            void DefineComboBox_DrawItem(object sender, DrawItemEventArgs e)
            {
                if (e.Index < 0)
                    return;
                ComboBox combo = sender as ComboBox;
                string combotext = combo.Text;
                <span style="color:#ff0000;"><strong>int index = combo.FindStringExact(combotext);
                //if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
                if (e.Index == index)</strong></span>
                    e.Graphics.FillRectangle(new SolidBrush(HighlightColor),
                                             e.Bounds);
                else
                    e.Graphics.FillRectangle(new SolidBrush(combo.BackColor),
                                             e.Bounds);

                e.Graphics.DrawString(combo.Items[e.Index].ToString(), e.Font,
                                      new SolidBrush(Color.Black),
                                      new Point(e.Bounds.X, e.Bounds.Y));
                e.DrawFocusRectangle();
            }
        }


——————————————————————————————————————————————————————————————————

欢迎大神光临菜鸟博客,希望能得到各位大神在编码方面的指引,同时欢迎与我一样刚进入编程世界的朋友一起讨论学习!相信前进的道路上,有你们,编程世界会更加精彩!





  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值