Limit the number of rows in a textbox

2

Is it possible to limit the amount of rows in a% multiline% by using C # .NET 3.5?

    
asked by anonymous 07.02.2014 / 18:47

1 answer

4

There is no property limiting the number of lines of TextBox , only the number of characters.

Nothing, however, prevents you from implementing the KeyDown event by counting lines and preventing a new one from being created when you reach the limit.

Assuming you are using winforms:

int maxLines = 3;        
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        if (textBox1.Text.Split('\n').Length >= maxLines)
        {
            e.SuppressKeyPress = true;
        }
    }
}

Remembering that it would be necessary to set the property WordWrap to false to prevent TextBox from displaying a new line when the text reaches the end.

    
07.02.2014 / 18:55