Limit the maximum number of characters per line in the text box

Say I have the following:

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

Is there a way to limit the maximum number of characters per line to 60?

I have seen ways to do this using keydown events, but this does not seem to be reliable (e.g. how to insert in a long block of text).

0
source share
5 answers

, keydown, , , TextBox "60 ". , TextChanged TextBox. .

(edit) , , : , 60 , , . , , .

+1

. 60 .

+1

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

,
, :)

0

, , , , , , , , 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 , , , .

EDIT. wpf,

0

I looked at several solutions today. Either they do not work at all, or if they work, they do not work as I think. Oddities with cursor position or incorrect hyphenation. Here is my “solution”, and I hope that it helps someone in the future. It wraps, does not expand, and saves any existing newlines. I set 60 characters wide for this example, and bool isBusyUpdating outside this example so that it does not start again when the update is in progress.

        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;

            }

        };
0
source

Source: https://habr.com/ru/post/1611780/


All Articles