C# WinForms 组合框中突出显示的文本

use*_*423 5 c# combobox highlighting winforms

无论如何,是否有办法阻止 winforms 组合框中的所选项目在设置时自动突出显示?(通过组合框属性 SelectedIndex 或 SelectedItem)。

所发生的情况是,我们用一组项目填充组合框,然后为要在组合中显示的项目设置索引,但这会导致文本突出显示。当组合框被禁用时,这意味着很难阅读文本,因为突出显示颜色为蓝色而文本颜色为白色。看起来这种行为是设计使然,但非常烦人!

我在网上找到并尝试过的唯一可行的解​​决方案是对组合框控件进行子类化,但这太具有侵入性,并且意味着我们必须替换应用程序中的所有组合框才能解决此问题。我还尝试在加载父控件后将组合上的 SelectionLength 属性设置为 0,并尝试在组合框中调用 Select(0,0),但都没有达到预期的效果。

有任何想法吗?

谢谢

Aft*_*med 0

这会起作用

private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
   // Draw the background.
   e.DrawBackground();
  // Determine the forecolor based on whether or not
  // the item is selected.
  Brush brush;
  // Get the item text.
  string text = ((ComboBox)sender).Items[e.Index].ToString();
  if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
  {
    brush = Brushes.White;
  }
     // Draw the text.
    e.Graphics.DrawString(text, ((Control)sender).Font, brush, e.Bounds.X, e.Bounds.Y);
}
Run Code Online (Sandbox Code Playgroud)