winforms 如何更改列表框选择内容的背景色?

kh212irz  于 2023-11-21  发布在  其他
关注(0)|答案(5)|浏览(247)

它似乎使用了Windows设置中的默认颜色,默认情况下是蓝色的。假设我想将其永久更改为红色。我使用的是Winforms。
先谢了。

vvppvyoh

vvppvyoh1#

必须重写Drawitem事件并将DrawMode属性设置为DrawMode.OwnerDrawFixed
检查此示例:

  1. private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
  2. {
  3. if (e.Index < 0) return;
  4. // If the item is selected them change the back color.
  5. if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
  6. e = new DrawItemEventArgs(e.Graphics,
  7. e.Font,
  8. e.Bounds,
  9. e.Index,
  10. e.State ^ DrawItemState.Selected,
  11. e.ForeColor,
  12. Color.Yellow); // Choose the color.
  13. // Draw the background of the ListBox control for each item.
  14. e.DrawBackground();
  15. // Draw the current item text
  16. e.Graphics.DrawString(listBox1.Items[e.Index].ToString(),e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault);
  17. // If the ListBox has focus, draw a focus rectangle around the selected item.
  18. e.DrawFocusRectangle();
  19. }

字符串


的数据

展开查看全部
ccrfmcuu

ccrfmcuu2#

希望这将有助于有人在未来的上述代码帮助我,但不是100%
我仍然有以下问题:

  • 当我选择另一个索引时,新选择的索引也会亮红色。
  • 当我改变列表框的字体大小时,高亮区域会太小。
    下面解决这个问题
  • 将DrawMode更改为ownerdrawvariable
  • 为列表框创建MeasurItem和DrawItem事件
  1. private void lstCartOutput_MeasureItem(object sender, MeasureItemEventArgs e)
  2. {
  3. // Cast the sender object back to ListBox type.
  4. ListBox listBox = (ListBox)sender;
  5. e.ItemHeight = listBox.Font.Height;
  6. }
  7. private void lstCartOutput_DrawItem(object sender, DrawItemEventArgs e)
  8. {
  9. ListBox listBox = (ListBox)sender;
  10. e.DrawBackground();
  11. Brush myBrush = Brushes.Black;
  12. if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
  13. {
  14. myBrush = Brushes.Red;
  15. e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(0, 64, 64)), e.Bounds);
  16. }
  17. else
  18. {
  19. e.Graphics.FillRectangle(Brushes.White, e.Bounds);
  20. }
  21. e.Graphics.DrawString(listBox.Items[e.Index].ToString(),e.Font, myBrush, e.Bounds);
  22. e.DrawFocusRectangle();
  23. }

字符串
我还参考了MSDN网站。

展开查看全部
yzckvree

yzckvree3#

下面的代码做的正是你所说的:
在InitializeComponent方法中:

  1. this.listBox1.DrawMode = DrawMode.OwnerDrawFixed;
  2. this.listBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(listBox1_DrawItem);
  3. this.listBox1.SelectedIndexChanged += new System.EventHandler(listBox1_SelectedIndexChanged);

字符串
和事件处理程序:

  1. void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
  2. {
  3. this.listBox1.Invalidate();
  4. }
  5. void listBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
  6. {
  7. int index = e.Index;
  8. Graphics g = e.Graphics;
  9. foreach (int selectedIndex in this.listBox1.SelectedIndices)
  10. {
  11. if (index == selectedIndex)
  12. {
  13. // Draw the new background colour
  14. e.DrawBackground();
  15. g.FillRectangle(new SolidBrush(Color.Red), e.Bounds);
  16. }
  17. }
  18. // Get the item details
  19. Font font = listBox1.Font;
  20. Color colour = listBox1.ForeColor;
  21. string text = listBox1.Items[index].ToString();
  22. // Print the text
  23. g.DrawString(text, font, new SolidBrush(Color.Black), (float)e.Bounds.X, (float)e.Bounds.Y);
  24. e.DrawFocusRectangle();
  25. }


代码取自:
http://www.weask.us/entry/change-listbox-rsquo-selected-item-backcolor-net

展开查看全部
hvvq6cgz

hvvq6cgz4#

为什么一开始就使用ListBox,而不是用一个完全可定制的单列、无标题可见、只读的DataGridView来代替它呢?

n1bvdmb6

n1bvdmb65#

我也有同样的问题。
不幸的是,我的数组是一个实体类的列表。所以我有相同的代码与上面接受的答案,但有轻微的修改,以选择我的类上的确切属性,我需要在我的列表框的DrawString上:

  1. if (e.Index < 0) return;
  2. if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
  3. e = new DrawItemEventArgs(e.Graphics,
  4. e.Font,
  5. e.Bounds,
  6. e.Index,
  7. e.State ^ DrawItemState.Selected,
  8. e.ForeColor,
  9. Color.Yellow);
  10. e.DrawBackground();
  11. //This is my modification below:
  12. e.Graphics.DrawString(ctListViewProcess.Items.Cast<entMyEntity>().Select(c => c.strPropertyName).ElementAt(e.Index), e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault);
  13. e.DrawFocusRectangle();

字符串

展开查看全部

相关问题