Resize textbox control base on font and max length in vb.Net

The purpose for this code is to dynamically change the textbox width base on its max length and font. This is a slightly modified code Provided by: FMS Development Team

Resize a Control to Fit Text

There are times when you want a form to dynamically resize one or more controls to fit their entire text. For example, given a specific text value, you want a textbox that grows wide enough to show the entire string. .NET provides various measurement functions in the System.Graphics library to accomplish this.

The following example code shows how to make a textbox wide enough to accommodate its text.

// C#
   // Determine the correct size for the text box based on its text length
   // Create a new SizeF object to return the size into
   System.Drawing.SizeF mySize = new System.Drawing.SizeF();
   // Create a new font based on the font of the textbox we want to resize
   System.Drawing.Font myFont = new System.Drawing.Font(this.textBox1.Font.FontFamily, this.textBox1.Font.Size);
   //
   // Or, use this for a specific font and font size.
   // System.Drawing.font myFont = new System.Drawing.Font("Verdana", 8);

   // Get the size given the string and the font
   mySize = e.Graphics.MeasureString("This is a test", myFont);

   // Resize the textbox to accommodate the entire string
   // Me.TextBox1.Width = mySize.Width;
   this.textBox1.Width = (int) Math.Round(mySize.Width, 0);

' VB
   ' Determine the correct size for the text box based on its text length
   ' Create a new SizeF object to return the size into
   Dim mySize As New System.Drawing.SizeF

   ' Create a new font based on the font of the textbox we want to resize
   Dim myFont As New System.Drawing.Font _
     (Me.TextBox1.Font.FontFamily, Me.TextBox1.Font.Size)
   '
   ' Or, use this for a specific font and font size.
   ' Dim myFont As New System.Drawing.Font("Verdana", 8)

   ' Get the size given the string and the font
   mySize = e.Graphics.MeasureString("This is a test", myFont)

   ' Resize the textbox to accommodate the entire string
   'Me.TextBox1.Width = mySize.Width
   Me.TextBox1.Width = CType(Math.Round(mySize.Width, 0), Integer)

And for the modified code:

    Private Sub MeasureTextBoxWidth(ByVal maxLength As Integer, ByRef tBox As TextBox)

        Dim str As String
        Dim mySize As New System.Drawing.SizeF
        'Dim myFont As New System.Drawing.Font(tBox.Font.FontFamily, tBox.Font.Size)
        Dim myFont = New Font(FontFamily.GenericMonospace, 10.0F)
        tBox.Font = myFont

        str = New String("A"c, maxLength)
        Dim instance As Graphics = tBox.CreateGraphics()
        ' Get the size given the string and the font
        mySize = instance.MeasureString(str, myFont)

        ' Resize the textbox to accommodate the entire string
        tBox.Width = CType(Math.Round(mySize.Width, 0), Integer)
    End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>