1
votes

I have a WPF C# program where I attempt to delete certain characters from a text box at TextChanged event. Say, for instance, the dollar sign. Here is the code I use.

private void txtData_TextChanged(object sender, TextChangedEventArgs e)
{
      string data = txtData.Text;

      foreach( char c in txtData.Text.ToCharArray() )
      {
            if( c.ToString() == "$" )
            {
                  data = data.Replace( c.ToString(), "" );
            }
      }

      txtData.Text = data;
}

The problem I have is that whenever the user enters $ sign (Shift + 4), at the TextChanged event it removes the $ character from the textbox text alright, but it also moves the cursor to the BEGINNING of the text box which is not my desired functionality.

As a workaround I thought of moving the cursor the the end of the text in the text box, but the problem there is that if the cursor was positioned at some middle position then it would not be very user friendly. Say, for instance the text in the textbox was 123ABC and if I had the cursor after 3, then moving the cursor to the end of the text would mean that at the next key stroke user would enter data after C, not after 3 which is the normal functionality.

Does anybody have an idea why this cursor shift happens?

4
Unrelated but do you know that you can do: txtData.Text.Replace("$", "") which will replace all occurences of "$"? No need for that whole loop. - ChrisWue
You might also want to look at the PreviewTextInput event. - Paul Walls
Thanks Paul, yes I finally ended up using PreviewTextInpu where I avoid all the unnecessary characters. - Sach

4 Answers

4
votes

Its not an answer to your question, but probably a solution for your problem:

How to define TextBox input restrictions?

If it is overkill for you, set e.Handled = true for all characters you want to avoid in PreviewKeyDown (use Keyboard.Modifiers for SHIFT key) or PreviewTextInput.

Try TextBox.CaretIndex for restoring cursor position in TextChanged event.

Hope it helps.

3
votes

You can use the Select function of TextBox to change the cursor position.

private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
    textBox1.Text = textBox1.Text.Replace("$", "");            
    textBox1.Select(textBox1.Text.Length, 0);
}

You can see more about Position the Cursor on the MSDN

0
votes

You can use the SelectionStart property of the textbox. Probably something along these lines should work:

private void txtData_TextChanged(object sender, TextChangedEventArgs e)
{
  var pos = txtData.SelectionStart;
  string data = txtData.Text.Replace("$", "");
  txtData.Text = data;
  txtData.SelectionStart = pos;
}
0
votes

You can try Regular Expression Sample

1) Use PreviewTextInput="CursorIssueHandler" in .xaml file

2) In your .cs file ,write the below code:

    private void CursorIssueHandler(object sender, TextCompositionEventArgs e)
    {
        var TB = (sender as TextBox);
        Regex regex = new Regex("[^0-9a-zA-Z-]+");
        bool Valid = regex.IsMatch(e.Text);
        //System.Diagnostics.Debug.WriteLine(Valid); // check value for valid n assign e.Handled accordingly your requirement from regex
        e.Handled = Valid;
     }