Redraw item items in The ListBox control in C #

Source: Internet
Author: User
"How to make the item items of ListBox display multiple rows ?", It seems that no one gives a direct answer.
My personal experience is summarized as follows:
1. First, select the ListBox control dragged to the Panel, click Properties, select drawmode, and change it to ownerdrawfixed or ownerdrawvariable.
2. In the property tool, switch to the event (that is, the lightning icon) and double-click drawitem under the 'Action' menu to add an event.
3. A new event is added to the 'code generated by the Form Designer '.
This. listbox1.drawitem + = new system. Windows. Forms. drawitemeventhandler (this. listbox#drawitem );
4. The learned friends should know that there will be a listboxmediadrawitem () method in the Code. Write the code in it.
 
Private void listboxincludrawitem (Object sender, system. Windows. Forms. drawitemeventargs E)
{
E. drawbackground ();
Brush mybrush = brushes. Black; // initialize font color = black
This. listbox1.itemheight = 90; // set the item height. Set the value as needed.
// Set the font color for each item
// You can skip this switch if you do not need it.
Switch (E. Index)
{
Case 0:
Mybrush = brushes. Red;
Break;
Case 1:
Mybrush = brushes. Orange;
Break;
Case 2:
Mybrush = brushes. Purple;
Break;
Case 4:
Mybrush = brushes. White;
Break;
}
E. Graphics. drawstring (listbox1.items [E. Index]. tostring (), E. Font, mybrush, E. bounds, null );
// This sentence does not seem necessary. Try it by yourself.
E. drawfocusrectangle ();
}
Actually, if you have read msdn, you should know that there is similar code in msdn, But I have modified a few places,
This. listbox1.itemheight = 90 is added. You can enter multiple lines of characters. Note that,
The height of the entire ListBox should be a multiple of the items you set. Otherwise, the ListBox will be distorted when displayed!
(For example, if my item height is 60, and ListBox needs to display three items at a time, it is set to 184)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.