Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2006-05-17
2
Medium Priority
?
349 Views
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.
0
Comment
Question by:hlominac
  • 2
2 Comments
 
LVL 6

Expert Comment

by:ctm5
ID: 16705752
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
            Label1.Refresh()
            len = Label1.Width
            If len > savelen Then savelen = len
        Next
        ListBox1.Width = savelen
0
 
LVL 6

Accepted Solution

by:
ctm5 earned 1000 total points
ID: 16705754
Oh, and same method should work for an RTB.

ctm5
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month21 days, 1 hour left to enroll

810 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