CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2008
    Posts
    48

    WPF (with C#) TextBox Cursor Position Problem

    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.

    Code:
    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?

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: WPF (with C#) TextBox Cursor Position Problem

    Probably the easiest way to handle this is to not let the unwanted characters into the text box to begin with.

    To filter characters, handle the PreviewTextInput event and set the e.Handled property to true to exclude characters.

    The code below excludes "B" or "b" characters. Notice how the cursor position remains the same (when the user types B or b, it's just ignored).
    Code:
        public partial class Window1 : Window
        {
            public Window1( )
            {
                InitializeComponent( );
    
                textBox1.PreviewTextInput += textBox1_PreviewTextInput;
            }
    
            static void textBox1_PreviewTextInput( object sender, TextCompositionEventArgs e )
            {
                var s = e.Text;
    
                if( e.Text.ToUpper() == "B")
                {
                    e.Handled = true;
                }
    
                Debug.WriteLine(s);
            }
        }
    Note: The Debug.WriteLine statement is optional. I just added it because I didn't know if the e.Text property
    included the whole string or just the entered character. If you open the Output window while debugging, it's
    a handy way to see what's going on.
    Last edited by Arjay; September 1st, 2011 at 09:22 PM.

  3. #3
    Join Date
    Jul 2008
    Posts
    48

    Re: WPF (with C#) TextBox Cursor Position Problem

    Thank you ArJay.

    Actually I was suggested the exact same thing by a poster in some other forum, and it worked just perfect for me.

    The only glitch here is that it still lets spaces in as the below doesn't work.

    Code:
    if( e.Text.ToUpper() == " ")
    I managed to fix this by checking for space key at PreviewKeyDown and setting e.Handled to true if it is the space key. That prevents from space being entered.

    And just in case if anyone else reading this is interested I also found a workaround to handle two-byte chars. I'm using a Japanese OS so it is critical that I handle them as well. All I had to do is in PreviewKeyDown check for Key.OemProcessed which is a key used to mask normal keys when in IME mode. I set e.Handled to true if the KeyEventArgs.Key is equal to OemProcessed and that prevents from user entering two-byte characters.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: WPF (with C#) TextBox Cursor Position Problem

    Glad you got it figured out.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured