我需要限制要粘贴到多行文本框中的字符数。
假设这是我要粘贴到文本框中的字符串:
Good day Ladies and Gents!
I just want to know
如果可以,请帮忙。
规则是每行最大字符数为 10,最大行数为 2。应用该规则,粘贴的文本只能是这样的:
Good day L
I just wan
最佳答案
不会自动执行此操作。您需要处理文本框上的 TextChanged
事件,并手动解析更改后的文本以将其限制为所需的格式。
private const int MaxCharsPerRow = 10;
private const int MaxLines = 2;
private void textBox1_TextChanged(object sender, EventArgs e) {
string[] lines = textBox1.Lines;
var newLines = new List<string>();
for (int i = 0; i < lines.Length && i < MaxLines; i++) {
newLines.Add(lines[i].Substring(0, Math.Min(lines[i].Length, MaxCharsPerRow)));
}
textBox1.Lines = newLines.ToArray();
}
关于c# - 如何限制在文本框中粘贴的字符数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17737602/