Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit number of lines in .net textbox

I am using the winforms textbox with multiline option ON. I want to limit the number of lines that can be entered in it. User should not be able to enter lines more than that.

How can I achieve that?

like image 304
Ramesh Soni Avatar asked Jun 26 '09 10:06

Ramesh Soni


2 Answers

You need to check for

txtbox.Lines.Length

You need to handle this for 2 scenarios: 1. User is typing in the textbox 2. User has pasted text in the textbox

User typing in textbox

You need to handle the key press event of the text box to prevent user from entering more lines when max lines are exceeded.

private const int MAX_LINES = 10;

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (this.textBox1.Lines.Length >= MAX_LINES && e.KeyChar == '\r')
    {
        e.Handled = true;
    }
}

I have tested the above code. It works as desired.

User pastes some text in the textbox

To prevent the user from pasting more than the max lines, you can code the text changed event handler:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    if (this.textBox1.Lines.Length > MAX_LINES)
    {
        this.textBox1.Undo();
        this.textBox1.ClearUndo();
        MessageBox.Show("Only " + MAX_LINES + " lines are allowed.");
    }
}
like image 170
SO User Avatar answered Sep 30 '22 03:09

SO User


This solution doesn't work because when you type continuously, it will be treated as 1 line irrespective of the no of lines you see on the screen.

In order to resolve the same, you need to use the SendMessage API to count the no of lines you see on the screen. Here is the code.

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

Along with this, you need to find out the cursor position in the text box, so that you can allow the user to type. In the previous code, once it reaches to 13 lines, user won't be able to type in any line. In order to overcome that, you have to find out, the cursor is in which line. Use the below code for that.

Declare this along with the API declaration

Private Const EM_LINEFROMCHAR = &HC9

Below code must be placed in MouseDown, MouseUp, KeyDown and KeyUp events of the textbox.

intLineNo = SendMessageINT(txtText1.Handle, EM_LINEFROMCHAR, -1, 0&) + 1

After finding out the LineNo, you can do the evaluation in the KeyPress event of the TextBox.

like image 32
Leo Leslin Avatar answered Sep 30 '22 01:09

Leo Leslin