哪个 CheckedListBox 类型控件(在 devexpress 中)允许单个项目的颜色更改?

发布于 2024-12-17 08:12:24 字数 222 浏览 0 评论 0原文

我正在使用 CheckedListBox ,但我需要使用不同的颜色/字体或其他突出显示方法突出显示各个项目。

但它只允许更改整个控件的视觉属性,而不能更改单个项目的视觉属性。

其他信息: 我正在使用 2 个这样的列表(因为需要复选框以便于选择)和 2 个按钮(>> <<)用于包含/排除类型功能。还有其他更好的方法来实现我的上述要求吗?

I am using CheckedListBox , but I need to highlight individual items with different colors/fonts or some other highlighting method.

But it allows to change visual properties for the whole control only and not for individual items.

Additional Info:
I am using 2 such lists (because need checkboxes for easier selection) and 2 buttons (>> <<) for include/exclude type functionality. Is there any other better way to implement such whereby my requirement as above also gets fulfilled ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江湖正好 2024-12-24 08:12:24

我只涉足 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;
}

I've only dabbled with DevExpress controls, but I think you have to subscribe to the DrawItem event and set the e.Handled property to true.

Something like this (CheckedListBox items with different colors):

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文