您的位置:首页 > 其它

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

2015-01-15 22:49 417 查看
在输入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();
}
}


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

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


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息