lothos12345
Junior Contributor
Is there away to limit the number of lines in a textbox using visual basic.net? Is so how?
Private Sub txtAddress_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtAddress.KeyPress
If AscW(e.KeyChar) = 8 Then 'backspace
e.Handled = False
Exit Sub
End If
If txtAddress.Text.IndexOf(System.Environment.NewLine) > 0 Then
Dim address() As String = Split(txtAddress.Text, System.Environment.NewLine)
If address.GetUpperBound(0) > 1 Then
If address.GetUpperBound(0) >= 2 _
And AscW(e.KeyChar) = 13 Then 'enter key
e.Handled = True
Else
Dim g As Graphics = Me.CreateGraphics
If g.MeasureString(address(2), txtAddress.Font).Width >= txtAddress.Width Then
e.Handled = True
Else : e.Handled = False
End If
g.Dispose()
End If
End If
End If
End Sub