How to Make Text Auto Size in Textbox Using VB.Net

This Tutorial is all about How to Make Text Auto Size in Textbox Using VB.Net. In this tutorial you will be able to learn How to Make Text Auto Size in Textbox Using VB.Net. So lets get Started:

  • First is open the Visual Basic, Select File on the menu, then click New and create a new project.

  • Then a New Project Dialog will appear. You can rename your project, depending on what you like to name it. After that click OK

  • After that, design your form like this just like what I’ve shown you below.
    Add a Textbox in a Form.
  • Then add this following declarations.
    [vbnet]
    Dim h As String
    Dim w As String
    [/vbnet]
  • Add this Paint Event handler code.
    [vbnet]
    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    Dim orgFont As New Font(TextBox1.Font.Name, TextBox1.Font.Size, TextBox1.Font.Style)
    Dim textSize As New SizeF
    textSize = e.Graphics.MeasureString(TextBox1.Text, orgFont)
    h = textSize.Height
    w = textSize.Width
    End Sub
    [/vbnet]
  • Then add this code to the textbox.
    [vbnet]
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    TextBox1.Font = New Font(TextBox1.Font.Name, 8, TextBox1.Font.Style)
    TextBox1.BorderStyle = BorderStyle.Fixed3D
    Do Until w > TextBox1.Size.Width – 5 Or TextBox1.Text = Nothing Or h > 182
    TextBox1.Font = New Font(TextBox1.Font.Name, TextBox1.Font.Size + 2, TextBox1.Font.Style)
    Loop
    TextBox1.BorderStyle = BorderStyle.None
    End Sub
    [/vbnet]
  • Finally, Click F5 to run the Program

Output:

If you have any comments or suggestions about on How to Make Text Auto Size in Textbox Using VB.Net, please feel free to contact our webpage.

Download How to Make Text Auto Size in Textbox Using VB.Net Here

Other Articles Readers might read:

Leave a Comment