Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在windows窗体应用程序中,限制多行文本框中每行的字符数_C#_Winforms_Textbox - Fatal编程技术网

C# 在windows窗体应用程序中,限制多行文本框中每行的字符数

C# 在windows窗体应用程序中,限制多行文本框中每行的字符数,c#,winforms,textbox,C#,Winforms,Textbox,如何在windows窗体应用程序中限制多行文本框中每行的字符数 我在文本框的KeyPress事件中做了一些类似的事情,但它不会转到行开头的新行 if (txtNewNotes.Text.Length % 50 == 0) txtNewNotes.Text += Environment.NewLine; 您正在检查的属性(txtNewNotes.Text)用于文本框中的所有文本(所有行中的所有文本组合)。 您需要检查的是txtNewNotes.Lines属性,该属性返回文本框中包含的每一行的字

如何在windows窗体应用程序中限制多行文本框中每行的字符数

我在文本框的
KeyPress
事件中做了一些类似的事情,但它不会转到行开头的新行

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

您正在检查的属性(
txtNewNotes.Text
)用于文本框中的所有文本(所有行中的所有文本组合)。
您需要检查的是
txtNewNotes.Lines
属性,该属性返回文本框中包含的每一行的
字符串[]
。遍历这些字符串并检查其长度

请记住,对于当前代码,您只需在文本框的末尾添加新行。

您需要处理用户返回到文本框中间的一行的情况,并开始编辑一行,使该特定行比您的50字符限制长,并且在文本框的结尾处产生不希望的新行数

您正在检查的属性(<代码> txtNeNek.Text < /代码>)。用于文本框中的所有文本(所有行中的所有文本组合)。
您需要检查的是
txtNewNotes.Lines
属性,该属性返回文本框中包含的每一行的
字符串[]
。遍历这些字符串并检查其长度

请记住,对于当前代码,您只需在文本框的末尾添加新行。

您需要处理用户返回文本框中间的一行的情况,并开始编辑一行,使该特定行比您的50字符限制长,并且在文本框的结尾处产生新的行的不希望的数量

以始终在行的末尾使用以下代码:

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;
}

然而,这种实现仍然有许多缺陷。例如,如果您手动更改行,此解决方案将不会注意到它。您可能需要使用txtNewNotes.Lines来跟踪一行中的字符数。

要始终在行尾,请使用以下代码:

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;
}

然而,这种实现仍然有许多缺陷。例如,如果您手动更改行,此解决方案将不会注意到它。您可能希望使用txtNewNotes.Lines来跟踪一行中的字符数。

来限制每行文本框中的文本

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;
            }
        }
private void txtNewNotes\u KeyDown(对象发送者,KeyPressEventArgs e)
{
if(txtNewNotes.Text.Length==0)返回;
如果(e.KeyChar=='\r')
{
e、 已处理=错误;
返回;
}
如果(e.KeyChar=='\b')
{
e、 已处理=错误;
返回;
}
int index=txtNewNotes.GetLineFromCharIndex(txtNewNotes.SelectionStart);
string temp=txtNewNotes.Lines[索引];
if(temp.Length<45)//字符限制
{
e、 已处理=错误;
}
其他的
{
e、 已处理=正确;
}
}

当用户复制和粘贴文本时,需要处理一件事,即不应用字符限制来限制文本框中每行使用的文本

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;
            }
        }
private void txtNewNotes\u KeyDown(对象发送者,KeyPressEventArgs e)
{
if(txtNewNotes.Text.Length==0)返回;
如果(e.KeyChar=='\r')
{
e、 已处理=错误;
返回;
}
如果(e.KeyChar=='\b')
{
e、 已处理=错误;
返回;
}
int index=txtNewNotes.GetLineFromCharIndex(txtNewNotes.SelectionStart);
string temp=txtNewNotes.Lines[索引];
if(temp.Length<45)//字符限制
{
e、 已处理=错误;
}
其他的
{
e、 已处理=正确;
}
}

当用户复制并粘贴文本时,需要处理一件事,即不应用字符限制

拼接文本
答案也会起作用。这个
text
答案也可以。