繁体   English   中英

限制文本框中每行的最大字符数

[英]Limit the max number of chars per line in a textbox

说我有以下内容:

<TextBox TextWrapping="Wrap" 
         AcceptsReturn="True" 
         AcceptsTab="True" 
         MaxLines="3000"/>

有没有办法可以将每行的最大字符数限制为60?

我已经看到了通过keydown事件做到这一点的方法,但这似乎并不是万无一失的(即在长文本块中粘贴的内容)。

选择单声道间隔字体。 并计算具有60个字符的文本框的宽度。

鉴于您正在响应keydown事件,我假设您要确保TextBox后面的字符串遵循“每行60个字符”规则。 如果是这种情况,您应该在TextBox上创建一个订阅TextChanged事件的事件。 在那里你可以修复文本,并截断或拆分太长的行。

(编辑)要解决显示部分,你可以像Kafuka建议的那样:只需将盒子放宽到足以容纳60个字符,如果你想确定,可以使用等宽字体。 如果你确定字符串是正确的,这应该很容易排成一行。

我真的不认为你可以在包装时这样做,因为换行会改变当前行以适应TextBox 即使在使用记事本时 ,也无法在启用Word Wrap时查看状态栏 ,因为在行时很难获得当前行索引及其长度。

我已设法设置每行的最大字符数,而TextWrapping属性设置为NoWrap 您首先需要获取当前行索引的长度。 然后,如果它是59或更多,处理输入。

<TextBox Name="textBox1"
         TextWrapping="NoWrap" 
         AcceptsReturn="True" 
         AcceptsTab="True" 
         MaxLines="3000"
         KeyDown="textBox1_KeyDown"/>
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    //Initialize a new int of name CurrentLine to get the current line the user is on
    int CurrentLine = textBox1.GetLineIndexFromCharacterIndex(textBox1.Text.Length);

    //Continue if the length of the current line is more or equal to 59
    if (textBox1.GetLineLength(CurrentLine) >= 59) 
    {
        //Don't insert the character
        e.Handled = true; 
    }
}

谢谢,
我希望这个对你有用 :)

我知道这是一个非常晚的答案,但是找到这个的人可以得到一个很好的答案,例如Ctrl + C和东西:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    foreach (string line in textBox1.Lines)
    {
        if (line.Length > 60)
        {
            textBox1.Undo();
        }
    }

    textBox1.ClearUndo();
}

请注意,这确实意味着您不能再在文本框中使用Ctrl + Z,但如果这不打扰您,这是一个不错的选择,因为它适用于任何字体。

编辑这不适用于wpf文本框,只有Windows形成文本框

我今天看了几个解决方案。 他们要么根本不工作,要么他们工作,他们不按我认为应该的方式工作。 光标位置的奇数或包装不正确。 这是我的“解决方案”,我希望它能帮助将来的某个人。 它包装,不打开,并保留任何现有的新行。 我将此示例设置为60个字符宽,并且在此示例之外使用bool isBusyUpdating以防止在更新进行时再次触发。

        txtNotes.HorizontalContentAlignment = HorizontalAlignment.Left;
        txtNotes.VerticalContentAlignment = VerticalAlignment.Top;
        txtNotes.TextWrapping = TextWrapping.NoWrap;
        txtNotes.AcceptsReturn = true;
        txtNotes.TextChanged += delegate (object o, TextChangedEventArgs args)
        {
            //args.Handled = true;
            TextBox thisText = (TextBox)args.Source;
             if (!isBusyUpdating)
            {
                isBusyUpdating = true;
                string updatedText = "";
                bool blnUpdate = false;
                int curPos = thisText.CaretIndex;

                foreach (string thisLine in thisText.Text.Split('\n'))
                {

                    if (thisLine.Length > 60)
                    {
                        blnUpdate = true;
                        updatedText = updatedText + 
                            thisLine.Substring(0, 60)
                            .Replace("\n", "") +
                                      "\n" + thisLine.Substring(60);
                        curPos++;
                    }
                    else
                    {
                        updatedText = updatedText + thisLine + "\n";
                    }
                }

                if (blnUpdate)
                {
                    thisText.Text = updatedText;
                    thisText.CaretIndex = curPos-1;
                }
                isBusyUpdating = false;

            }

        };

暂无
暂无

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

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