Get the 'size' of a line of text to set width of control

  • Thread starter Thread starter Tom
  • Start date Start date
T

Tom

I want to get the 'size' of a line of text so that I can set the width of a
control to show all that text. For instance, say my text is 'The dog chased
the cat around the room' and I want to set a Button control's width equal to
the length of this text so that all the text shows up on the button (I know,
some controls have auto-sizing, but I need to do this manually at run time).

How can one determine and translate the 'size' of a text string into the
proper width for a control? Thanks.

Tom
 
Hi Tom, try this...

\\
Dim txt As String = "My shoes are too tight!"
Dim g As Graphics = myButton.CreateGraphics
Dim controlSize As SizeF = g.MeasureString(txt, selectGrid.Font)

myButton.Width = CInt(controlSize.Width) ' option strict on requires
this cast
myButton.Height = CInt(controlSize.Height)
myButton.Text = txt
//

hope this helps,

jim
 
* "Tom said:
I want to get the 'size' of a line of text so that I can set the width of a
control to show all that text. For instance, say my text is 'The dog chased
the cat around the room' and I want to set a Button control's width equal to
the length of this text so that all the text shows up on the button (I know,
some controls have auto-sizing, but I need to do this manually at run time).

Have a look at the 'Graphics.MeasureString' method.
 
Back
Top