Multiline Listbox?

  • Thread starter Thread starter Max
  • Start date Start date
M

Max

I need some control that basically acts as a listbox, but allows each
item to take up more then one line. I've tried adding a new item and
putting in a vbCrLf in there, but that just comes out as a box on the
same line. Is there some way of making the listbox show multiple lines
for each item, or is there another control (3rd party maybe) that will
let me do this?
 
* Max said:
I need some control that basically acts as a listbox, but allows each
item to take up more then one line. I've tried adding a new item and
putting in a vbCrLf in there, but that just comes out as a box on the
same line. Is there some way of making the listbox show multiple lines
for each item, or is there another control (3rd party maybe) that will
let me do this?

You will have to draw the items yourself. Have a look at the listbox's
'DrawMode' property.
 
Hi,

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
For x As Integer = 0 To 20
ListBox1.Items.Add(String.Format("Item {0} {1} Line 2", x,
ControlChars.NewLine))
Next
ListBox1.DrawMode = DrawMode.OwnerDrawVariable
End Sub


Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As
System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
Dim g As Graphics = e.Graphics
Dim s As String
Dim d As Date
Dim br As Brush = SystemBrushes.WindowText
Dim brBack As Brush
Dim rDraw As Rectangle
Dim bSelected As Boolean = CBool(e.State And
DrawItemState.Selected)

rDraw = e.Bounds
rDraw.Inflate(-1, -1)

If bSelected Then
brBack = Brushes.LightBlue
g.FillRectangle(Brushes.LightBlue, rDraw)
g.DrawRectangle(Pens.Blue, rDraw)
Else
brBack = Brushes.White
g.FillRectangle(brBack, e.Bounds)
End If

br = Nothing
brBack = Nothing
rDraw = Nothing
Try
s = ListBox1.Items.Item(e.Index).ToString
Catch
s = ""
End Try
Debug.WriteLine(String.Format("{0} {1} {2}", e.Index, s,
e.Bounds.ToString))

g.DrawString(s, ListBox1.Font, Brushes.Black,
RectangleF.op_Implicit(e.Bounds))
End Sub

Private Sub ListBox1_MeasureItem(ByVal sender As Object, ByVal e As
System.Windows.Forms.MeasureItemEventArgs) Handles ListBox1.MeasureItem
e.ItemHeight += e.ItemHeight
End Sub

Ken
-------------
 
Herfried said:
There is a basic sample in the documentation for 'ListBox.DrawMode'.

Here's what I found in there:

Private Sub listBox1_DrawItem(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
' Set the DrawMode property to draw fixed sized items.
ListBox1.DrawMode = DrawMode.OwnerDrawFixed
' Draw the background of the ListBox control for each item.
e.DrawBackground()
' Create a new Brush and initialize to a Black colored brush by default.
Dim myBrush As Brush

' Determine the color of the brush to draw each item based on the
index of the item to draw.
Select Case (e.Index)
Case 0
myBrush = Brushes.Red
Case 1
myBrush = Brushes.Orange
Case 2
myBrush = Brushes.Purple
End Select

' Draw the current item text based on the current Font and the custom
brush settings.
e.Graphics.DrawString(ListBox1.Items(e.Index), e.Font, myBrush, New
RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
' If the ListBox has focus, draw a focus rectangle around the
selected item.
e.DrawFocusRectangle()
End Sub

So I get how that works, I just use listBox1.items.add("Some text") and
the first time it paints the text red... But this still doens't help me
in making the thing work on multiple lines. Or am I missing something???
 
* Max said:
So I get how that works, I just use listBox1.items.add("Some text")
and the first time it paints the text red... But this still doens't
help me in making the thing work on multiple lines. Or am I missing
something???

In the listbox's 'MeasureItem' event, measure the string to be drawn for
an item using 'Graphics.MeasureString'. In the 'DrawItem' event, the
string is drawn using 'DrawString'. Always have a look at the 'e'
parameters of the event handlers.
 
Back
Top