Wordwrap in VB.Net listbox?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • the_mikado

    Wordwrap in VB.Net listbox?

    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.

  • Ken Tucker [MVP]

    #2
    Re: Wordwrap in VB.Net listbox?

    Hi,

    You would have to make your listbox owner drawn

    Public Class Form1

    Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
    System.EventArg s) Handles MyBase.Load
    ListBox1.DrawMo de = DrawMode.OwnerD rawVariable
    For x As Integer = 1 To 3
    ListBox1.Items. Add(String.Form at("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_DrawIt em(ByVal sender As Object, ByVal e As
    System.Windows. Forms.DrawItemE ventArgs) Handles ListBox1.DrawIt em
    Dim g As Graphics = e.Graphics
    Dim br As SolidBrush
    Dim s As String

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

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

    If CBool(e.State And DrawItemState.S elected) Then
    g.FillRectangle (Brushes.LightB lue, e.Bounds)
    End If

    br = New SolidBrush(Colo r.Black)

    g.DrawString(s, ListBox1.Font, br, _
    RectangleF.op_I mplicit(e.Bound s))

    br.Dispose()
    End Sub


    Private Sub ListBox1_Measur eItem(ByVal sender As Object, ByVal e As
    System.Windows. Forms.MeasureIt emEventArgs) Handles ListBox1.Measur eItem
    Dim g As Graphics = e.Graphics
    Dim s As String

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


    Ken
    -------------------
    "the_mikado " <craig.potter@a lcoa.com.au> wrote in message
    news:1137389212 .481661.73510@g 49g2000cwa.goog legroups.com...[color=blue]
    > 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.
    >[/color]


    Comment

    • Carlos J. Quintero [VB MVP]

      #3
      Re: Wordwrap in VB.Net listbox?

      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:
      MZ-Tools has a single goal: To make your everyday programming life easier. As an add-in to several Integrated Development Environment (IDEs) from Microsoft, MZ-Tools adds new menus and toolbars to them that provide many new productivity features.



      "the_mikado " <craig.potter@a lcoa.com.au> escribió en el mensaje
      news:1137389212 .481661.73510@g 49g2000cwa.goog legroups.com...[color=blue]
      > 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.
      >[/color]


      Comment

      • the_mikado

        #4
        Re: Wordwrap in VB.Net listbox?

        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.

        Comment

        • the_mikado

          #5
          Re: Wordwrap in VB.Net listbox?

          Thanks Ken, I will try this.

          Regards,
          Craig.

          Comment

          Working...