Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the display of multiple lines of Item items by ListBox in C #

Shulou Source: shulou.com Published: 2022-06-02 13:26:56 09月29日 Update

This article is about how ListBox implements the display of multiple lines of Item items in C #. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1. First select the ListBox control dragged to the panel, click Properties, select DrawMode, and change it to OwnerDrawFixed or OwnerDrawVariable.

2. Again in the Properties tool, switch to the event (the lightning icon) and double-click DrawItem under the behaviors menu to add an event.

3. A new event has been added to the code generated by the form designer

This.listBox1.DrawItem + = new System.Windows.Forms.DrawItemEventHandler (this.listBox1_DrawItem)

4. Friends who have learned should know that there will be a listBox1_DrawItem () method in the code, so write the code in it.

Private void listBox1_DrawItem (object sender, System.Windows.Forms.DrawItemEventArgs e) {e.DrawBackground (); Brush myBrush = Brushes.Black; / / initialize font color = black this.listBox1.ItemHeight=90 / / set item height. Set the value according to specific needs / / set the font color for each item / / you can not write this switch switch (e.Index) {case 0: myBrush = Brushes.Red; break; case 1: myBrush = Brushes.Orange if you don't need it. 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); / / if you don't need this sentence, try e.DrawFocusRectangle ();}

In fact, friends who have seen MSDN should know, there is similar code in MSDN, I just modified a few places, added item high this.listBox1.ItemHeight=90, you can enter multiple characters, it should be noted here that the height of the whole ListBox should be a little higher than the multiple of the Item item you set, otherwise ListBox will be distorted when displayed!

Thank you for reading! This is the end of this article on "how to display multiple lines of Item items in ListBox in C#". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Tags: Code multilines events clips content fonts properties more friends articles colors good practical multiples icons places characters that is tools controls Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Linux Redmi Shulou Technology Docker