我正在使用ComboBox DrawItem事件,但是当我使用它时,所选项目无效。
我的意思是我按下右键单击组合框中的特定项目,但随机的其他项目显示在ComboBox中的选定行上(虽然选择的实际项目很好,但它只显示其他项目被选中)
这是我的代码:
System.Windows.Forms.ComboBox comboBox;
comboBox = new System.Windows.Forms.ComboBox();
comboBox.AllowDrop = true;
comboBox.DrawMode = DrawMode.OwnerDrawFixed;
comboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
comboBox.FormattingEnabled = true;
comboBox.DrawItem += (sender, DrawItemEventArgs) =>
{
Font font = comboBox.Font;
Brush backgroundColor;
Brush textColor;
if (indexes.Contains(DrawItemEventArgs.Index))
{
if ((DrawItemEventArgs.State & DrawItemState.Selected)==DrawItemState.Selected)
{
backgroundColor = Brushes.Cyan;
textColor = Brushes.Black;
}
else
{
backgroundColor = Brushes.Brown;
textColor = Brushes.Black;
}
}
else
{
if ((DrawItemEventArgs.State & DrawItemState.Selected)==DrawItemState.Selected)
{
backgroundColor = SystemBrushes.Highlight;
textColor = SystemBrushes.HighlightText;
}
else
{
backgroundColor = SystemBrushes.Window;
textColor = SystemBrushes.WindowText;
}
}
DrawItemEventArgs.Graphics.FillRectangle(backgroundColor, DrawItemEventArgs.Bounds);
if (DrawItemEventArgs.Index == -1) return;
DrawItemEventArgs.Graphics.DrawString((sender as ComboBox).
Items[DrawItemEventArgs.Index].ToString(), font,textColor,DrawItemEventArgs.Bounds);
};
我的油漆有问题吗?因为我调试并发现这个事件正在制造麻烦。
TNX。