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

0
37

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.
    1. Dim h As String
    2. Dim w As String
  • Add this Paint Event handler code.
    1. Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    2. Dim orgFont As New Font(TextBox1.Font.Name, TextBox1.Font.Size, TextBox1.Font.Style)
    3. Dim textSize As New SizeF
    4. textSize = e.Graphics.MeasureString(TextBox1.Text, orgFont)
    5. h = textSize.Height
    6. w = textSize.Width
    7. End Sub
  • Then add this code to the textbox.
    1. Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    2. TextBox1.Font = New Font(TextBox1.Font.Name, 8, TextBox1.Font.Style)
    3. TextBox1.BorderStyle = BorderStyle.Fixed3D
    4. Do Until w > TextBox1.Size.Width - 5 Or TextBox1.Text = Nothing Or h > 182
    5. TextBox1.Font = New Font(TextBox1.Font.Name, TextBox1.Font.Size + 2, TextBox1.Font.Style)
    6. Loop
    7. TextBox1.BorderStyle = BorderStyle.None
    8. End Sub
  • 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:

 

 

 

Facebook Comments
(Visited 39 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here