This tutorial will show you how to set font size of Textbox to autosize means that when you type more words in Textbox, It will set the font size to fit the Textbox which contains these text.
No | Control Type | Name | Text | Event |
1 | Form1 | FormAutoSize | AutoSize Font | |
2 | Textbox1 | TextBoxAuto | Text_Changed |
Code
Public Class FormAutoSizeFont
Dim h As String
Dim w As String
Private Sub FormAutoSizeFont_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
Dim orgFont As New Font(TextBoxAutosize.Font.Name, TextBoxAutosize.Font.Size,
TextBoxAutosize.Font.Style)
Dim textSize As New SizeF
textSize =
e.Graphics.MeasureString(TextBoxAutosize.Text, orgFont)
h = textSize.Height
w = textSize.Width
End Sub
Private Sub TextBoxAutosize_TextChanged(sender As Object, e As EventArgs) Handles
TextBoxAutosize.TextChanged
TextBoxAutosize.Font = New Font(TextBoxAutosize.Font.Name,
12, TextBoxAutosize.Font.Style)
TextBoxAutosize.BorderStyle = BorderStyle.Fixed3D
Do Until w > TextBoxAutosize.Size.Width - 5 Or TextBoxAutosize.Text = Nothing Or h > 182
TextBoxAutosize.Font = New Font(TextBoxAutosize.Font.Name,
TextBoxAutosize.Font.Size + 2, TextBoxAutosize.Font.Style)
Loop
TextBoxAutosize.BorderStyle = BorderStyle.None
TextBoxAutosize.TextAlign = HorizontalAlignment.Center
TextBoxAutosize.WordWrap = True
End Sub
End Class
0 comments:
Post a Comment