How to change ListBox selection background color?

后端 未结 5 1438
执念已碎
执念已碎 2020-11-28 12:49

It seems to use default color from Windows settings which is blue by default. Let\'s say I want to change it to red permanently. I\'m using Winforms.

Thanks in advan

5条回答
  •  时光说笑
    2020-11-28 13:41

    I have the same problem.

    Unfortunately my datasource is a List of Entity Class. So I have the same code with the accepted answer above but with minor modification to select the exact property on my Class that I need on DrawString for my ListBox:

     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().Select(c => c.strPropertyName).ElementAt(e.Index), e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault);
      e.DrawFocusRectangle();
    

提交回复
热议问题