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

kh212irz  于 6个月前  发布在  其他
关注(0)|答案(5)|浏览(72)

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

vvppvyoh

vvppvyoh1#

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

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
    if (e.Index < 0) return;

    // If the item is selected them change the back color.
    if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
        e = new DrawItemEventArgs(e.Graphics, 
                                  e.Font, 
                                  e.Bounds, 
                                  e.Index,
                                  e.State ^ DrawItemState.Selected,
                                  e.ForeColor, 
                                  Color.Yellow); // Choose the color.

    // Draw the background of the ListBox control for each item.
    e.DrawBackground();

    // Draw the current item text
    e.Graphics.DrawString(listBox1.Items[e.Index].ToString(),e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault);

    // If the ListBox has focus, draw a focus rectangle around the selected item.
    e.DrawFocusRectangle();
}

字符串


的数据

ccrfmcuu

ccrfmcuu2#

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

  • 当我选择另一个索引时,新选择的索引也会亮红色。
  • 当我改变列表框的字体大小时,高亮区域会太小。
    下面解决这个问题
  • 将DrawMode更改为ownerdrawvariable
  • 为列表框创建MeasurItem和DrawItem事件
private void lstCartOutput_MeasureItem(object sender, MeasureItemEventArgs e)
{
   // Cast the sender object back to ListBox type.
   ListBox listBox = (ListBox)sender;
   e.ItemHeight = listBox.Font.Height;
}

private void lstCartOutput_DrawItem(object sender, DrawItemEventArgs e)
{
   ListBox listBox = (ListBox)sender;
   e.DrawBackground();
   Brush myBrush = Brushes.Black;

   if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
   {
      myBrush = Brushes.Red;
      e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(0, 64, 64)), e.Bounds);
   }

   else
   {
      e.Graphics.FillRectangle(Brushes.White, e.Bounds);

   }

   e.Graphics.DrawString(listBox.Items[e.Index].ToString(),e.Font, myBrush, e.Bounds);
   e.DrawFocusRectangle();
}

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

yzckvree

yzckvree3#

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

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

字符串
和事件处理程序:

void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
    this.listBox1.Invalidate();
}

void listBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
    int index = e.Index;
    Graphics g = e.Graphics;
    foreach (int selectedIndex in this.listBox1.SelectedIndices)
    {
        if (index == selectedIndex)
        {
            // Draw the new background colour
            e.DrawBackground();
            g.FillRectangle(new SolidBrush(Color.Red), e.Bounds);
        }
    }

    // Get the item details
    Font font = listBox1.Font;
    Color colour = listBox1.ForeColor;
    string text = listBox1.Items[index].ToString();

    // Print the text
    g.DrawString(text, font, new SolidBrush(Color.Black), (float)e.Bounds.X, (float)e.Bounds.Y);
    e.DrawFocusRectangle();
}


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

hvvq6cgz

hvvq6cgz4#

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

n1bvdmb6

n1bvdmb65#

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

if (e.Index < 0) return;
        if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
            e = new DrawItemEventArgs(e.Graphics,
                                      e.Font,
                                      e.Bounds,
                                      e.Index,
                                      e.State ^ DrawItemState.Selected,
                                      e.ForeColor,
                                      Color.Yellow);

  e.DrawBackground();
  //This is my modification below:
  e.Graphics.DrawString(ctListViewProcess.Items.Cast<entMyEntity>().Select(c => c.strPropertyName).ElementAt(e.Index), e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault);
  e.DrawFocusRectangle();

字符串

相关问题