Im trying to automatically (in the form_load event) set the width of every textbox in my form, I want to set this property according to the maxlength property of the textbox, this way every textbox is gonna have exactly the width it needs to display the bigger string it needs to display, I used this code snippet, but it doesnt work for every font (if use a bold font it stops working, for example):
private sub form_load()
dim sampleTxtBox as TextBox
sampleTxtBox.maxlenght=50
heightWidthOfCtrl(generateString(sampleTxtBox.maxlenght), sampleTxtBox)
end sub
Private Function heightWidthOfCtrl(ByVal cad As String, ByRef ctrl As Control)
Dim g As Graphics
g = ctrl.CreateGraphics()
ctrl.Width = Convert.ToInt32(Math.Ceiling(g.MeasureString(cad, ctrl.Font).Width))
ctrl.Height = Convert.ToInt32(Math.Ceiling(g.MeasureString(cad, ctrl.Font).Height))
End Function
Private Function generateString(ByVal t As Integer) As String
Dim strTmp As String
Dim i As Integer
strTmp = ""
For i = 1 To t
strTmp = strTmp + "X"
Next i
generateString = strTmp
End Function
does anyone know another way to do this
I hope someone can help me...
Gustavo

Is there anyway to set the width of a textbox according to its maxlength property???
Domino
though since the question was never answered here it is
FYI: bug: you must fillschema of the datatable before fill in order to get this
dataset1.tables("Hello").datacolumns(0).maxlength
I believe that's what it looks like.
If you don't do fillschema it will return -1
Visualcpp
First off, I think it would look a bit sloppy. I can't think of a UI I've ever seen with different width TextBoxes (that are next to each other).
But also, your theory would only work with fixed-width fonts. With most fonts, the ending string would be a totally different width for every different character combination.
So I guess if I were you, I'd probably rethink my strategy on that one.