Is there a way to control the number of lines in a multi-line textbox. I want to have just one textbox and let it be only 2 lines max, but I need to allow the user to press "enter" to display text on the next line. Any thoughts?
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles TextBox1.KeyPress
Dim s As Integer
Dim sf As Rectangle
Dim numLines As Integer '-- Get font height
s = TextBox1.Font.Height() '-- Get client size
sf = TextBox1.ClientRectangle '-- Get max visible lines
numLines = sf.Height \ s '-- Reject CR if
If numLines > TextBox1.Lines.Length Then
e.Handled = False
ElseIf e.KeyChar = Chr(13) Then
e.Handled = True
End If
End Sub
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim s As Integer
Dim sf As Rectangle
Dim numLines As Integer
'-- Get font height
s = TextBox1.Font.Height()
'-- Get client size
sf = TextBox1.ClientRectangle
'-- Get max visible lines
numLines = sf.Height \ s
Dim start_point As Integer = TextBox1.SelectionStart
If e.KeyChar = Chr(8) Then
e.Handled = False
Else
If TextBox1.Lines.Length = 1 Then
If TextBox1.Text.Length > 42 Then
If (e.KeyChar = Chr(13) Or TextBox1.Text.Length >= 84) Then
e.Handled = True
End If
End If
ElseIf TextBox1.Lines.Length = 2 Then
Dim stop_flag As Boolean = False
Dim line1_text As String = "", line2_text As String = ""
Dim line1_cnt As Integer = 0, line2_cnt As Integer = 0
line1_text = TextBox1.Lines(0)
line2_text = TextBox1.Lines(1)
If line1_text.Length >= 42 Then
If start_point <= 42 Then
e.Handled = True
ElseIf line2_text.Length >= 42 Then
e.Handled = True
End If
ElseIf line2_text.Length >= 42 Then
If line1_text.Length >= 42 Then
e.Handled = True
ElseIf start_point > line1_text.Length Then
e.Handled = True
End If
End If
If e.KeyChar = Chr(13) Then
e.Handled = True
End If
End If
End If
End Sub