HighTechTalks DotNet Forums  

Multiline Listbox?

VB.net microsoft.public.dotnet.languages.vb


Discuss Multiline Listbox? in the VB.net forum.

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Max
 
Posts: n/a

Default Multiline Listbox? - 06-02-2004 , 05:29 PM






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?



Reply With Quote
  #2  
Old   
AT
 
Posts: n/a

Default Re: Multiline Listbox? - 06-02-2004 , 05:49 PM






* Max <maximkh (AT) yahoo (DOT) com> scripsit:
Quote:
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/>


Reply With Quote
  #3  
Old   
Max
 
Posts: n/a

Default Re: Multiline Listbox? - 06-02-2004 , 05:51 PM



Herfried K. Wagner [MVP] wrote:

Quote:
* Max <maximkh (AT) yahoo (DOT) 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?


Reply With Quote
  #4  
Old   
AT
 
Posts: n/a

Default Re: Multiline Listbox? - 06-02-2004 , 06:57 PM



* Max <maximkh (AT) yahoo (DOT) com> scripsit:
Quote:
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/>


Reply With Quote
  #5  
Old   
Ken Tucker [MVP]
 
Posts: n/a

Default Re: Multiline Listbox? - 06-02-2004 , 07:51 PM



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" <maximkh (AT) yahoo (DOT) com> wrote

Quote:
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




Reply With Quote
  #6  
Old   
Max
 
Posts: n/a

Default Re: Multiline Listbox? - 06-02-2004 , 08:24 PM



Herfried K. Wagner [MVP] wrote:

Quote:
* Max <maximkh (AT) yahoo (DOT) 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???


Reply With Quote
  #7  
Old   
AT
 
Posts: n/a

Default Re: Multiline Listbox? - 06-03-2004 , 02:29 AM



* Max <maximkh (AT) yahoo (DOT) com> scripsit:
Quote:
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/>


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.