I have vb.net application and have a richtextbox..
I want set the lines limit to 12 and after 12 lines the user will not allow to enter text.
How can i do this?

You can use RTB.Lines.Count() method to get total lines number. It gives you the total count on every press of Return Key.

I have the code for counting the lines , but i want to prevent the user to edit data beyond 12 lines. How can i do that?

I have got this code but I am not able to clear my problem.

Private Declare Function SendMessageINT Lib "user32" Alias "SendMessageA" _
        (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Private Const EM_GETLINECOUNT = &HBA

Private Sub txtText1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtText1.KeyPress

    Const MAX_LINES = 13
    Dim lngCount As Long

    lngCount = SendMessageINT(txtText1.Handle, EM_GETLINECOUNT, 0, 0)

    If lngCount = MAX_LINES And Asc(e.KeyChar) <> Keys.Back And Asc(e.KeyChar) <> Keys.Delete Then
        e.Handled = True
    End If
End Sub

In the above code I can only stop user to enter in 13th line but not in the 14th line and beyond that....

I'd use the simpler Me.Text = first 12 lines of text (your code goes here) to strip the rest. Your specification can be taken many ways. I'm reading that they can enter 12 lines, but you strip the rest.

Try the KeyDown event instread

Private Sub RichTextBox1_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyDown

    If e.KeyCode = Keys.Enter And RichTextBox1.Lines.Count = MAXLINES Then
        e.SuppressKeyPress = True
    End If

End Sub

Edited 1 Year Ago by Reverend Jim

I applied the code as per Reverend Jim but still does not get my problem to be solved.

Satyam, I read your requirements a few times but came away that they were not complete. I might make a bet you didn't really mean to disable input once the 12 lines were hit but that you wanted to allow them to edit the first 12 and automatically discard line 13 and on.

@rproffitt - the code I suggested allows all of that. It says only to disallow any ENTER keys if already at the max allowed. All other keystrokes are still allowed.

@satyam_1 - please explain how the code does not meet your needs and I will try to modify it accordingly.

@Reverend Jim. My bet is their spec is incomplete. That is they are thinking display lines and not ENTER terminated lines. satyam_1 needs to elaborate.

Yeah. It was ambiguous but considering that the number of displayed lines would vary depending on the size of the form I assumed hard terminated lines as the more likely scenario.

Finally I modified the as: and its perfactly working for e.g. for 2 lines.

Thanks Reverend Jim & rproffitt....

Private Declare Function SendMessageINT Lib "user32" Alias "SendMessageA" _
        (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    Private Const EM_GETLINECOUNT = &HBA
    Private Sub RichTextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyDown

        Const MAXLINES = 2
        If e.KeyCode = Keys.Enter And RichTextBox1.Lines.Count = MAXLINES Then
            e.SuppressKeyPress = True
        End If
    End Sub

    Private Sub RichTextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles RichTextBox1.KeyPress
        Const MAX_LINES = 3
        Dim lngCount As Long
        lngCount = SendMessageINT(RichTextBox1.Handle, EM_GETLINECOUNT, 0, 0)
        If lngCount = MAX_LINES And Asc(e.KeyChar) <> Keys.Back And Asc(e.KeyChar) <> Keys.Delete Then
            e.Handled = True
        End If
    End Sub
This question has already been answered. Start a new discussion instead.