Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Textbox autosize to fit the contents

Status
Not open for further replies.

drimades

IS-IT--Management
Nov 8, 2004
221
MK
How can I force a textbox to grow in height so to fit the the contents of a text field?
 
set the text box to multi-line. Then count the number of characters that fit in a horizontal line for you textbox width... then set the height property to something like (22 * (txtBox.Text.Length / YourNumberOfCharsPerLine)). It may also be helpful to set the font to one where all of the characters are the same width (such as "Courier New").


It's not perfect, but it should be enough to get you started. Set it up and test the heck out of it.


Senior Software Developer
 
It works great! I have 15 textboxes in my form. How can I loop to set the properties for all the textboxes in the form?
 
You can loop into your form's controls collection:

Something similar to:
Code:
For each box as TextBox in me.Controls
  box.property = someValue
En For

hope it helps!
 
I'm using:
Code:
Dim box As TextBox
For Each box In Me.Controls
    box.Width = 400
Next

and it gives a compile error. The wizard has assigned automatically names like: "editName", "editPhone" etc. to the textboxes. How can I refer to them (in a loop I mean)?
 
I don't think that you can assign each control to a textbox variable. You probably want something closer to:

Code:
For Each c as Control in Me.controls

    If TypeOf c Is TextBox Then
  
        c.width = 400

    End If

Next

This example will only set the width on textboxes within your controls. The loop is taking each control and allowing you to access them one at a time. If you want to access the boxes by name, you can check the .Name property in the loop, or you can access them directly by their variable names outside of a loop.

--Rob
 
As a word of caution, that will only work with properties that Control has; to use properties specific to TextBox you would need something like:

[tt]
For Each c as Control in Me.controls
If TypeOf c Is TextBox Then
With CType(c, TextBox)
.ChosenProperty = RequiredValue
....
....
End With
End If
Next
[/tt]


Hope this helps

[vampire][bat]
 
My mistake! I'm still half a cup of coffee away from that solution :).

--Rob
 
Not at all, your answer was fine because (as far as I am aware) Control has Height and Width properties - which in essence was the original requirement.

[vampire][bat]
 
To answer the original question:

Code:
  'This requires :
  '  Four TextBoxes called tb1, tb2, tb3 and tb4
  '  MultiLine set to true
  '  WordWrap set to True

  Private LongString As String = _
    "This is a string that is longer than the width of the textboxes.  It will therefore " + _
    "wrap onto more than one line.  Additionally this string will be repeated based on the TextBox name."

  Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

    tb1.Text = LongString
    tb2.Text = LongString + "  " + LongString
    tb3.Text = tb2.Text + "  " + LongString
    tb4.Text = tb2.Text + "  " + tb2.Text

    For Each c As Control In Me.Controls
      If TypeOf c Is TextBox Then
        With CType(c, TextBox)
          Dim g As Graphics = CreateGraphics()
          Dim sz As SizeF = g.MeasureString(.Text, .Font, .ClientSize.Width)
          .Height = CType(sz.Height, Integer)
        End With
      End If
    Next

  End Sub

Hope this helps.

[vampire][bat]
 
I forgot to include (for neatness):

[tt]
g = Nothing
sz = Nothing
[/tt]

which will go immediately before [tt]End With[/tt]

[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top