1

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?

Sach
  • 10,091
  • 8
  • 47
  • 84
  • 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 Aug 31 '11 at 00:43
  • 1
    You might also want to look at the `PreviewTextInput` event. – Paul Walls Aug 31 '11 at 01:52
  • Thanks Paul, yes I finally ended up using PreviewTextInpu where I avoid all the unnecessary characters. – Sach Sep 05 '11 at 05:16

4 Answers4

4

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.

Community
  • 1
  • 1
LPL
  • 16,827
  • 6
  • 51
  • 95
3

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

slavoo
  • 5,798
  • 64
  • 37
  • 39
Shebin
  • 3,310
  • 2
  • 24
  • 27
  • Sach prefers cursor on previous position, not at the end. – LPL Aug 31 '11 at 11:27
  • Agree to LPL, I want to emulate the exact functionality you get when you type into any normal text box. Only, if it's $ (there are other characters I want to avoid too) then I don't want it do be displayed, AND I want the cursor to remain where it was. – Sach Sep 01 '11 at 06:24
0

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;
     }
tomerpacific
  • 4,704
  • 13
  • 34
  • 52
0

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;
}
ChrisWue
  • 18,612
  • 4
  • 58
  • 83
  • This doesn't work either. It for some reason moves the cursor to the next position after I hit Shift+$. – Sach Sep 01 '11 at 06:25