Home All Groups Group Topic Archive Search About

Wordwrap in VB.Net listbox?

Author
16 Jan 2006 5:26 AM
the_mikado
Hi, If this has been answered before I am sorry, but I want to wordwrap
the items in a listbox so I dont have to use the horizontal scroll bar.
Is this possible?  if so how can it be done?

Regards,
Craig.

Author
16 Jan 2006 12:02 PM
Ken Tucker [MVP]
Hi,

        You would have to make your listbox owner drawn

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
        ListBox1.DrawMode = DrawMode.OwnerDrawVariable
        For x As Integer = 1 To 3
            ListBox1.Items.Add(String.Format("Line {0} that is way way way
to long to fit inside the listbox with out a scroll bar", x))
        Next
    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 br As SolidBrush
        Dim s As String

        Try
            s = ListBox1.Items.Item(e.Index).ToString
        Catch ex As Exception
            Trace.WriteLine(ex.ToString)
            s = "error"
        End Try

        g.FillRectangle(Brushes.White, e.Bounds)

        If CBool(e.State And DrawItemState.Selected) Then
            g.FillRectangle(Brushes.LightBlue, e.Bounds)
        End If

        br = New SolidBrush(Color.Black)

        g.DrawString(s, ListBox1.Font, br, _
            RectangleF.op_Implicit(e.Bounds))

        br.Dispose()
    End Sub


    Private Sub ListBox1_MeasureItem(ByVal sender As Object, ByVal e As
System.Windows.Forms.MeasureItemEventArgs) Handles ListBox1.MeasureItem
        Dim g As Graphics = e.Graphics
        Dim s As String

        Try
            s = ListBox1.Items.Item(e.Index).ToString
        Catch ex As Exception
            s = "error"
        End Try
        Dim sz As SizeF = g.MeasureString(s, ListBox1.Font, ListBox1.Width _
                - 5 - SystemInformation.VerticalScrollBarWidth)
        e.ItemHeight = CInt(sz.Height) + 5
        e.ItemWidth = CInt(sz.Width) + 15
    End Sub
End Class


Ken
-------------------
Show quoteHide quote
"the_mikado" <craig.pot***@alcoa.com.au> wrote in message
news:1137389212.481661.73510@g49g2000cwa.googlegroups.com...
> Hi, If this has been answered before I am sorry, but I want to wordwrap
> the items in a listbox so I dont have to use the horizontal scroll bar.
> Is this possible?  if so how can it be done?
>
> Regards,
> Craig.
>
Author
16 Jan 2006 9:37 PM
the_mikado
Thanks Ken, I will try this.

Regards,
Craig.
Author
16 Jan 2006 3:49 PM
Carlos J. Quintero [VB MVP]
Hi Craig,

Apart from Ken's correct answer, can you consider making resizable the
window containing the list? While most people hate horizontal scrolling, a
listbox with wordwrap may appear even stranger, and what most people really
would like is to resize the windows of the apps to use the large monitors of
today... just a suggestion.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com


Show quoteHide quote
"the_mikado" <craig.pot***@alcoa.com.au> escribió en el mensaje
news:1137389212.481661.73510@g49g2000cwa.googlegroups.com...
> Hi, If this has been answered before I am sorry, but I want to wordwrap
> the items in a listbox so I dont have to use the horizontal scroll bar.
> Is this possible?  if so how can it be done?
>
> Regards,
> Craig.
>
Author
16 Jan 2006 9:36 PM
the_mikado
Thanks to all who replied. The reason I need wordwrap is that we are
implementing a touchscreen listbox with large listbox items & fonts.  A
horizontal toolbar is impractical but the text strings are too long.

Reagrds,
Craig.