473,396 Members | 1,945 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,396 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 11672
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.