Max lines in a multi-line textbox

wcsloco

Member
Joined
Feb 7, 2005
Messages
14
Programming Experience
5-10
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?
 
Actually I've came across the same problem too...I think my way around it would be to use labels with the background set to the form, rather then a text box, and use a geturl command with the label click event that has the "Enter" text on it- and obviously make the labels next to each other so it looks like a continuos line.
 
I did some searching an another form, and found this, which does mostly work. The only remaining issue I have is to disable wordwrap (which would allow a 3rd line of text in my example).
VB.NET:
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
 
Last edited by a moderator:
wcsloco, do take some care when posting, the forum provides you great editing tools, you may also edit your post afterwards when you see that what you posted looked like *garbage*, just click the 'Edit' button, also here you may click the 'Go Advanced' with better posting tools like previews and more of the like. I've edited your post this time, adding code box and formatted the code more properly, next time you help everybody by ensuring this so we may help you in return. Else I'm deleting such posts as spam!

Setting your user accounts Primary Platform is highly recommended. Do you use .Net 1 or 2 ?
 
Sorry about the lack of formatting. I'll make note of it for future posts. After spending some time on it, I now have this working exactly as I want. I first had to change the font of my textbox to be Courier New, so every character was the same spacing. For my example, I had a textbox that would have no more than 42 characters on a given line. I wanted to allow the user to either let the text wrap to the second line, or allow them to hit enter and record text on the second line. This code now only allows at most 42 characters per line, and at most, 84 characters in the entire textbox.

VB.NET:
 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
 
Although this does work if you change the font to Courier New, there must be a more generic way to handle it, regardless of the font. Can anyone help with that? I'd like to keep my font Microsoft Sans Serif, if possible. Thanks!
 
Back
Top