2

When the user adds a ;, I want to add ; + Environment.NewLine in the TextBox.

I find this solution :

private void TextBox_OnPreviewTextInput(object sender, TextCompositionEventArgs e)
{
    if (e.Text == ";")
    {
        e.Handled = true;
        TextCompositionManager.StartComposition(
                new TextComposition(InputManager.Current,
                (IInputElement)sender,
                ";" + Environment.NewLine)
        );
    }
}

But after this, the undo don't work.

Can you explain me how control the user input and keep the undo stack?

2 Answers 2

2

-------------- Updated Code As Requested ---------

Use this Instead and 100% it's work. I test it for assurance.

private void TextBox_OnPreviewTextInput(object sender, TextCompositionEventArgs e)
{
    if (e.Text == ";")
    {
        // In this line remove preview event to  preventing event repeating
        ((TextBox)sender).PreviewTextInput -= TextBox_OnPreviewTextInput;

        // Whith this code get the current index of you Caret(wher you inputed your semicolon)
        int index = ((TextBox)sender).CaretIndex;

        // Now do The Job in the new way( As you asked)
        ((TextBox)sender).Text = ((TextBox)sender).Text.Insert(index, ";\r\n");

        // Give the Textbox preview Event again
        ((TextBox)sender).PreviewTextInput += TextBox_OnPreviewTextInput;

        // Put the focus on the current index of TextBox after semicolon and newline (Updated Code & I think more optimized code)
        ((TextBox)sender).Select(index + 3, 0);

        // Now enjoy your app
         e.Handled = true;
    }
}

Wish you bests , Heydar

Sign up to request clarification or add additional context in comments.

4 Comments

Close, undo work with this solution. But when I press ';' in middle of sentence, ";\r\n" are put in the end.
I also Updated my asnwer as you asked, be suer to copying all of it. not part of it. I changed the Puting focus on last index too
I did not get your meaning by the word "dislike magic 3". Since we put 3 character to our text, so we must take 3 step forward till the caret travel to after semicolon. It's compeletly logical. Could you explain your meaning more?
The main goal of answer is being understandable/readable. Why I prefer ";\r\n".Length. When I read a answer, I don't want put effort to understand logic out of the problem. It's principally philosophical. ;)
1

Thank to Heydar for the solution. I applied some improvements :

private void TextBox_OnPreviewTextInput(object sender, TextCompositionEventArgs e)
{
    if (e.Text == ";")
    {
        var textBox = (TextBox) sender;
        var selectStart = textBox.SelectionStart;
        var insertedText = ";" + Environment.NewLine;

        // In this line remove preview event to  preventing event repeating
        textBox.PreviewTextInput -= TextBox_OnPreviewTextInput;

        // Now do The Job
        textBox.Text = textBox.Text.Insert(selectStart, insertedText);

        // Give the TextBox preview Event again
        textBox.PreviewTextInput += TextBox_OnPreviewTextInput;

        // Put the focus after the inserted text
        textBox.Select(selectStart + insertedText.Length, 0);

        // Now enjoy your app
        e.Handled = true;
    }
}

2 Comments

Hi, Did you test it and does it work? See bro I done it more Direct and in less code with out safe case and with direct cast. Yes I accept that the safe cast is better for it safety , but direct cast is faster in writing and less in volume(The code I wrote is in just 6 line) and memory consuming and maybe faster in performance. Since you gonna use this event in TextBox event so the sender is not nothing else . it's just TextBox always, unless you add to an Other control.
This is an old proposal for improvement. You can ignore it. I edited the answer in this direction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.