473,326 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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.

Jan 16 '06 #1
4 11660
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
-------------------
"the_mikado" <cr**********@alcoa.com.au> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.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.

Jan 16 '06 #2
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
"the_mikado" <cr**********@alcoa.com.au> escribió en el mensaje
news:11*********************@g49g2000cwa.googlegro ups.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.

Jan 16 '06 #3
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.

Jan 16 '06 #4
Thanks Ken, I will try this.

Regards,
Craig.

Jan 16 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: lawrence | last post by:
When users enter urls or other long strings it can destroy the formatting of a page. A long url, posted in a comment, can cause page distortions that make the page unreadable, till the website...
6
by: Pierre Jelenc | last post by:
I need to wrap text with a negative indent, that is this: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad...
0
by: Richard Back | last post by:
Hi, I would like to print the contents of a textbox to a printer, and for the text to wordwrap. The text could be more than one pages worth so I need to track the pages. I can get it to...
0
by: Mike | last post by:
Hi All, I mistakenly just posted this to the VB group. Sorry for the cross-post. I am working with a textbox in C# which pulls a set of lines (stored in the database as text_type,...
1
by: louise raisbeck | last post by:
Hi there, I create a report online with basically a dump of loads of tables. One of them has 27 columns and as such it ignores the datagrid.width property and goes right along the screen in a...
2
by: yxq | last post by:
Hello, The length of Tooltip string is not fixed, how to wordwrap according to width size? Where to find a function to return Multi-lines string from a long string? Thank you
1
by: Cesar Ronchese | last post by:
Hi! I'm using a DataGridView to show a datatable that have many columns. When it displays data, the lines are applying wordwrap in some cells, because the grid is too wider to page. I'm...
0
by: the_mikado | last post by:
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? ...
0
by: Harvey Triana | last post by:
Hi -- How to do a GridView without wordwrap ? <HT/> Thanks
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.