Find the length of a word in a list box or a  rich text box

Posted on 2006-05-17
Last Modified: 2010-04-23
I have a list of words I wish to display vertically  both in a list box and in a rich text box (RTB).  The RTB is fully docked on a panel.  I wish to determine the width of the panel to accommodate all the words.  What is the best way to do this in VB.NET?  It needs to depend upon the font size of the word.
Question by:hlominac
    LVL 6

    Expert Comment

    Here's a way to do it in a listbox. There are a number of other ways, including subclassing and using API calls, but this method is quick and painless.

    On your form, place a label. Set Visible = False and AutoSize = True. Then set the font and size to be the same as the listbox's.

    Then you can iterate through the items in the list box and place each string as the text of the label. The label will autosize to fit the entire string. Measure the width of the label, and compare it to the longest one you've found so far. By the time you finish iterating through the list box items, you will have found the longest one. Then just set the width of the list box to match.

    For example:

         Dim str As String
         Dim len As Integer
         Dim savelen As Integer = 0
         For Each str In ListBox1.Items
                Label1.Text = str
                len = Label1.Width
                If len > savelen Then savelen = len
            ListBox1.Width = savelen
    LVL 6

    Accepted Solution

    Oh, and same method should work for an RTB.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now