简体   繁体   English

在Windows Form应用程序的多行文本框中限制每行字符

[英]Limit characters per line in the multiline textbox in windows form application

How to limit characters per line in the multiline textbox in windows form application 如何在Windows Form应用程序的多行文本框中限制每行的字符

I am doing some thing like this in KeyPress Event of textbox but it will not go to new line goes on start of the row 我在文本框的KeyPress事件中正在做类似的事情,但是它不会转到行首的新行

 if (txtNewNotes.Text.Length % 50 == 0) txtNewNotes.Text += Environment.NewLine;

The property that you are checking ( txtNewNotes.Text ) is for all the text in the TextBox (All the text from all the lines combined). 您要检查的属性( txtNewNotes.Text )用于TextBox中的所有文本(所有行中的所有文本组合在一起)。
What you will need to check is the txtNewNotes.Lines property that returns a string[] of each line that is contained inside your TextBox. 您需要检查的是txtNewNotes.Lines属性,该属性返回TextBox中包含的每行的string[] Iterate through those strings and check their lengths. 遍历这些字符串并检查其长度。

Remember that with your current code, you are only adding a new line at the end of the TextBox. 请记住,使用当前代码,您仅在TextBox的末尾添加了新行。
You will need to handle the case where a user goes back to a line in the middle of the TextBox and starts editing a line there, making that specific line longer than your 50 character limit and causing an undesired amount of new lines at the end of the TextBox 您将需要处理以下情况:用户返回到TextBox中间的一行并在那里开始编辑一行,从而使该特定行的长度超过您的50个字符的限制,并在该行的结尾导致不希望有的新行数量文字框

To get always at the end of the line use following code: 要始终保持在行尾,请使用以下代码:

if (txtNewNotes.Text.Length % 50 == 0 && textBox1.Text.Length >= 50)
{                               
   txtNewNotes.Text += Environment.NewLine;
   // This sets the caret to end
   txtNewNotes.SelectionStart = txtNewNotes.Text.Length;
}

however this implementation still haves many flaws. 但是这种实现仍然存在许多缺陷。 For example if you change line manually, this solution wont notice it. 例如,如果您手动更改线路,此解决方案将不会注意到它。 You might want to use txtNewNotes.Lines to follow how many characters there are on a line. 您可能要使用txtNewNotes.Lines来跟踪一行中有多少个字符。

To limit the text in textbox per row use 要限制每行中文本框中的文本,请使用

private void txtNewNotes_KeyDown(object sender, KeyPressEventArgs e)
        {
            if (txtNewNotes.Text.Length == 0) return;

            if (e.KeyChar == '\r')
            {
                e.Handled = false;
                return;
            }

            if (e.KeyChar == '\b')
            {
                e.Handled = false;
                return;
            }

            int index = txtNewNotes.GetLineFromCharIndex(txtNewNotes.SelectionStart);
            string temp = txtNewNotes.Lines[index];
            if (temp.Length < 45) // character limit
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

One thing need to be handled when user copy and paste the text it is not apply character limit 用户复制粘贴文本时需要处理的一件事是不应用字符数限制

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM