Attention: Webforms setting the with of a textbox, depending of the len of the string and the font

  • Thread starter Thread starter ton
  • Start date Start date
T

ton

How can I in VB.NET set the width of a label or textbox depending on the
length of the string and/or the font size.
In my control I'm showing a textbox in a tablecell, which is added to a
table row
after monitoring several textboxes takes two or even three lines, this is
not what i want.

So how can help me out

Ton
 
Hallo Ton,

Have a look at the graphics.measurestring method for that.

I hope this helps?

Cor
 
Hi Ton,

Did you try this.
\\\
Dim g As Graphics = Graphics.FromImage(New Bitmap(1, 1))
Dim stringSize As New SizeF(g.MeasureString _
("Mystringlength", New Font("Arial", 16)))
Dim mysize As Single = stringSize.Width
///

I also never used in this way, and therefore I do not know if the bitmap
size is big enough and the value you get is in pixels, but that you can in
my opinion recalculate to px or whatever you want.

I hope it helps, I was also curious if it did work on a webform because the
webform is not bitmap.

Cor
 
Back
Top