windows - Limit number of lines in .net textbox -


I'm using the winforms text box with multiline options. I want to limit the number of lines that can be entered into it, the user should not be able to enter more lines than that.

How can I get it?

you

txtbox.Lines.Length < / P>

You need to manage it for 2 scenarios: 1. The user has to type in the text box 2. The user has pasted the text in the text box

Typing the user in the text box

To stop you, the key of the text box is to handle the press event when the maximum lines are exceeded then the more lines User to enter.

  private const int MAX_LINES = 10; Private zero text box 1_kimpress (object sender, keypress eventsErgus E) {if (this.Texbox 1.lines> Lang & gt; = MAX_LINES & A.KEKER == '\ r') {e.Handled = true; }}  

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

User pastes some text in the text box

To prevent the user from pasting more than the maximum lines, you can code the text change event Handler:

  Private zero text box 1 tag (object sender, EventArgs e) {if (this.TtextBox1.Lines.Length> MAX_LINES) {this.textBox1.Undo (); This.textBox1.ClearUndo (); MessageBox.Show ("Only" + MAX_LINES + "lines are allowed."); }}  

Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -