0

I want to modify the Text, that is typed in the TextBox, before it is displayed, without looking on the Text, that is already typed.

Example: xaml:

<TextBox x:Name="tb" TextChanged="tb_TextChanged">
    my long text
</TextBox>

c#:

private void tb_TextChanged(object sender, TextChangedEventArgs e)
{
    TextBox tb = sender as TextBox;

    if (tb != null)
    {
        //save caretIndex
        int caretIndex = tb.CaretIndex;

        //modify Text //can be any modification
        //vvvvvvvvvvv Lines that I'm talking about

        tb.Text.ToLower(); //or: tb.Text.ToLower(); etc.

        char[] notAllowedChars = new char[] {'/t', '~', '#'}; //any other
        foreach (char c in notAllowedChars)
        {
            tb.Text = tb.Text.Replace(c, '_'); //replace unwanted characters
        }

        //^^^^^^^^^^^^ these lines modify the whole text

        //restore caretIndex
        tb.CaretIndex = caretIndex;
    }
}

In that example, the whole Text is going to be modified. But all of the other characters are already modified. So I don't want to go through them again. I want only to modify the changed text before it gets inserted.

I'm talking of 100.000+ Characters. That means, that the looking up all characters causes an unwanted performance issue.

Is there any solution or is it an impossible demand.

Rob
  • 26,989
  • 16
  • 82
  • 98
Michael1248
  • 150
  • 1
  • 12
  • 2
    I would question the wisdom of putting 100,000 characters in a `TextBox` to *start* with :) – BradleyDotNET Apr 13 '17 at 17:20
  • See marked duplicate. Just use `Lower` instead of `Upper`. – Peter Duniho Apr 13 '17 at 18:31
  • @BradleyDotNET :D you'r totally right. I want to display large text files and they can have more than 100.000 Characters :) – Michael1248 Apr 13 '17 at 18:51
  • @PeterDuniho I'm sorry missleading you. `tb.Text.ToLower();` was only an example of text modification – Michael1248 Apr 13 '17 at 18:53
  • There are a variety of solutions, depending on what _exactly_ you are trying to do. Stack Overflow is already full of answers describing various ways to filter and/or alter text input. Your question "is there any solution" is inherently too broad. Please do some research, make an attempt, and if you have some _specific_ difficulty, post a question that includes a good [mcve] showing exactly what you tried, and a detailed explanation of what specifically you're unable to solve. – Peter Duniho Apr 13 '17 at 18:56
  • @PeterDuniho Please undo the marking as a duplicate. – Michael1248 Feb 27 '18 at 16:09

1 Answers1

2

You can look at the TextChangedEventArgs to see the changes, and then modify the text again...here is some starting code:

private bool m_bInTextChanged;
private void tb_TextChanged(object sender, TextChangedEventArgs e)
{
    if (m_bInTextChanged)
        return;

    m_bInTextChanged = true;

    TextBox tb = sender as TextBox;

    if (tb != null)
    {
        //save caretIndex
        int caretIndex = tb.CaretIndex;

        if (e.Changes.Any())
        {
            var addedchanges = e.Changes.Where(tc => tc.AddedLength > 0);

            foreach (var added in addedchanges)
            {
                string stringchanged = tb.Text.Substring(added.Offset, added.AddedLength);

                tb.Select(added.Offset, added.AddedLength);

                tb.SelectedText = stringchanged.ToLower();
            }
        }

        //restore caretIndex
        tb.CaretIndex = caretIndex;
    }

    m_bInTextChanged = false;
}
Colin Smith
  • 12,375
  • 4
  • 39
  • 47
  • Thanks you so much :) Works fine. Actually I looked over `... e.Changes.Where(tc => tc.AddedLength > 0);` but couldn't get through it :) Thanks again. – Michael1248 Apr 13 '17 at 19:01