•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the VB.NET section within the Software Development category of DaniWeb, a massive community of 402,044 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,442 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our VB.NET advertiser: Programming Forums
Views: 146 | Replies: 2
![]() |
•
•
Join Date: Nov 2007
Posts: 11
Reputation:
Rep Power: 1
Solved Threads: 0
Hi,
I would like to know how to get the width of a textstring and/or how to substring a string to a given width (not length), if possible. The reason is that i use a multicolumn listbox and should know how to cut of the length of the strings so they dont exceeds the columns-width.
The most ideel will be to cut a textstring in a given width (not length). Any surgestions how to do this..?
I would like to know how to get the width of a textstring and/or how to substring a string to a given width (not length), if possible. The reason is that i use a multicolumn listbox and should know how to cut of the length of the strings so they dont exceeds the columns-width.
The most ideel will be to cut a textstring in a given width (not length). Any surgestions how to do this..?
•
•
Join Date: Dec 2002
Location: West Virginia
Posts: 375
Reputation:
Rep Power: 6
Solved Threads: 37
Try this:
It is a listbox with a button.
It is a listbox with a button.
Public Class Form1
Dim str As String = "abcdefghijklmnopqrstuvwxyz"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Add(TrimToSize("abcdefghijklmnopqrstuvwxyz"))
ListBox1.Items.Add(TrimToSize("This is a major line"))
ListBox1.Items.Add(TrimToSize("Thomas, is that you?"))
ListBox1.Items.Add(TrimToSize("It's alive!"))
End Sub
Private Function TrimToSize(ByVal Str As String) As String
Dim wid As Integer = ListBox1.ClientSize.Width
Dim g As Graphics = ListBox1.CreateGraphics
Dim siz As New SizeF
Do
siz = g.MeasureString(Str, ListBox1.Font)
If siz.Width > wid Then Str = Str.Substring(0, Str.Length - 1)
Loop While siz.Width > wid
g.Dispose()
Return Str
End Function
End Class Wayne
It is hard to understand how a cemetery can raise its burial rates and blame it on the cost of living.
It is hard to understand how a cemetery can raise its burial rates and blame it on the cost of living.
![]() |
•
•
•
•
•
•
•
•
DaniWeb VB.NET Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Other Threads in the VB.NET Forum
- Previous Thread: Question about checkboxes in VB.NET
- Next Thread: Post XML String to URL (HTTPPOST) & Web service to accept that string.


Linear Mode