我正在使用 CheckedListBox ,但我需要使用不同的颜色/字体或其他一些突出显示方法突出显示单个项目。
但它只允许更改整个控件的视觉属性,而不是单个项目。
附加信息: 我正在使用 2 个这样的列表(因为需要复选框以便于选择)和 2 个按钮(>> <<)用于包含/排除类型功能。还有其他更好的方法来实现我的上述要求吗?
我正在使用 CheckedListBox ,但我需要使用不同的颜色/字体或其他一些突出显示方法突出显示单个项目。
但它只允许更改整个控件的视觉属性,而不是单个项目。
附加信息: 我正在使用 2 个这样的列表(因为需要复选框以便于选择)和 2 个按钮(>> <<)用于包含/排除类型功能。还有其他更好的方法来实现我的上述要求吗?
我只涉足 DevExpress 控件,但我认为您必须订阅该DrawItem
事件并将该e.Handled
属性设置为 true。
像这样的东西(CheckedListBox 具有不同颜色的项目):
private void checkedListBoxControl1_DrawItem(object sender, ListBoxDrawItemEventArgs e) {
CheckedListBoxControl clbControl = sender as CheckedListBoxControl;
ButtonState state = ButtonState.Normal;
if (clbControl.GetItemChecked(e.Index))
state = ButtonState.Checked;
ControlPaint.DrawCheckBox(e.Graphics, new Rectangle(e.Bounds.X, e.Bounds.Y, 15, 15), state);
string itemText = e.Item.ToString();
Rectangle textRect = new Rectangle(e.Bounds.X + 15, e.Bounds.Y, e.Bounds.Width - 15, e.Bounds.Height);
if ((e.State & DrawItemState.Selected) != 0) {
e.Graphics.FillRectangle(SystemBrushes.Highlight, textRect);
}
if (state== ButtonState.Checked)
e.Graphics.DrawString(itemText, e.Appearance.Font, new SolidBrush(Color.Red), textRect, e.Appearance.GetStringFormat());
else
e.Graphics.DrawString(itemText, e.Appearance.Font, new SolidBrush(Color.Black), textRect, e.Appearance.GetStringFormat());
e.Handled = true;
}