User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Nov 2007
Posts: 11
Reputation: sniper1983 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
sniper1983 sniper1983 is offline Offline
Newbie Poster

Width of a textstring

  #1  
Jul 9th, 2008
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..?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2002
Location: West Virginia
Posts: 375
Reputation: waynespangler is on a distinguished road 
Rep Power: 6
Solved Threads: 37
waynespangler waynespangler is offline Offline
Posting Whiz

Re: Width of a textstring

  #2  
Jul 9th, 2008
Try this:
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.
Reply With Quote  
Join Date: Nov 2007
Posts: 11
Reputation: sniper1983 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
sniper1983 sniper1983 is offline Offline
Newbie Poster

Re: Width of a textstring

  #3  
Jul 11th, 2008
Thanks, I'll try this later and get back to you.. The idea seems great..
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb VB.NET Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the VB.NET Forum

All times are GMT -4. The time now is 11:27 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC