Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can ListBox items span multiple lines? C# [duplicate]

Tags:

c#

.net

winforms

I'd like to have a ListBox control contain items that span multiple lines.

Essentially what I want is each item to span multiple lines and be selectable as one item. Is there a way to do this?

like image 420
CPS Avatar asked Jul 23 '26 14:07

CPS


1 Answers

As suggested by LarsTech in his comment, all the other comments lead to some kind of fully coded example which may confuse you. I made this demo with just some lines of code for you to follow and get started easily:

 listBox1.DrawMode = DrawMode.OwnerDrawVariable;
 //First add some items to your listBox1.Items     
 //MeasureItem event handler for your ListBox
 private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)
 {
   if (e.Index == 2) e.ItemHeight = 50;//Set the Height of the item at index 2 to 50
 }
 //DrawItem event handler for your ListBox
 private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
 {
   e.DrawBackground();
   e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds);
 }

enter image description here

like image 185
King King Avatar answered Jul 26 '26 04:07

King King



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!