By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,022 Members | 1,379 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,022 IT Pros & Developers. It's quick & easy.

Multiline Listbox?

P: n/a
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?
Nov 20 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
* Max <ma*****@yahoo.com> scripsit:
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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #2

P: n/a
Max
Herfried K. Wagner [MVP] wrote:
* Max <ma*****@yahoo.com> scripsit:

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.


Never used that property before, how does it work?
Nov 20 '05 #3

P: n/a
* Max <ma*****@yahoo.com> scripsit:
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.


Never used that property before, how does it work?


There is a basic sample in the documentation for 'ListBox.DrawMode'.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #4

P: n/a
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
-------------

"Max" <ma*****@yahoo.com> wrote in message
news:Ne********************@comcast.com:
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?


--
Outgoing mail is certified Virus Free.
Checked by AVG Anti-Virus (http://www.grisoft.com).
Version: 7.0.230 / Virus Database: 262.10.6 - Release Date: 5/28/2004
Nov 20 '05 #5

P: n/a
Max
Herfried K. Wagner [MVP] wrote:
* Max <ma*****@yahoo.com> scripsit:

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.

Never used that property before, how does it work?


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???
Nov 20 '05 #6

P: n/a
* Max <ma*****@yahoo.com> scripsit:
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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.