你需要检查
@H_502_1@txtBox.Lines.Length
@H_502_1@您需要处理以下两种情况:1.用户正在输入文本框2.用户已将文本粘贴到文本框中
@H_502_1@用户在文本框中输入
@H_502_1@您需要处理文本框的按键事件,以防止用户在超过最大行时输入更多行.
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; } }@H_502_1@我已经测试了上面的代码.它按照需要工作. @H_502_1@用户粘贴文本框中的一些文本 @H_502_1@为了防止用户粘贴超过最大行,您可以对文本更改的事件处理程序进行编码:
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."); } }