Set the font and color of a list item using code in C #

I am busy with a custom list, which I use as a read recorder in C #. Now I want to set a specific element in a specific element with a different font and color than the rest. I checked this question , and from the answers I made the following code:

private void myListBox_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { e.DrawBackground(); Font myFont; Brush myBrush; int i = e.Index; if (e.Index == 3) { myFont = e.Font; myBrush = Brushes.Black; } else { myFont = new Font("Microsoft Sans Serif", 9.75f, FontStyle.Bold); myBrush = Brushes.CadetBlue; } e.Graphics.DrawString(myListBox.Items[i].ToString(), myFont, myBrush, e.Bounds, StringFormat.GenericDefault); } 

And call the method in my IntializeComponent () using

 this.myListBox.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.myListBox_DrawItem); 

The call does not raise any exception, but I do not see any changes in the line that I want to process. Is something missing?

+4
source share
1 answer

You are missing another line in IntializeComponent() add this:

 this.myListBox.DrawMode = DrawMode.OwnerDrawFixed; 

before joining an event.

+5
source

Source: https://habr.com/ru/post/1487882/


All Articles