WPF (with C#) TextBox Cursor Position Problem

how to set cursor position in textbox in c# wpf
c# textbox cursor position change event
wpf textbox cursor
how to set cursor position in textbox in c# winforms
how to set cursor position in textbox in asp.net c#
vb net set textbox cursor position
vba get cursor position in textbox
vba set cursor position in textbox

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?

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.

WPF (with C#) TextBox Cursor Position Problem, It happens because you changed the text. I'd handle this by using this code: Boolean shifted = false; private void  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

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

WPF (with C#) TextBox Cursor Position Problem, 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  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.

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;
}

WPF (with C#) TextBox Cursor Position Problem, WPF (with C#) TextBox Cursor Position Problem RRS feed I have a WPF C# program where I attempt to delete certain characters from a text  I am try to set the caret/cursor position to the end of the string value in my WPF textbox when I open my window for the first time. I use the FocusManager to set the focus on my textbox when my window opens.

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;
     }

[Solved] Cursor position in a text box in wpf c#, There is some mixing up going on in most of times between the terms "caret" and "cursor". Cursor is the mouse cursor, caret is the vertical  Instead, the cursor in this context is the object that declares the onscreen graphical display related to several possible input-related devices or concepts in Windows programming. That object is represented by the Cursor class in WPF. The WPF input system enables you to change this cursor when it represents the onscreen position of the mouse

C cursor position, I recently posted a question on "How to set cursor position in a Picturebox" (​Q_21241276. I was thinking of just get the x and y of the textbox curosr than add the x and y static extern bool SetCursorPos(int X, int Y); in WPF to set the mouse position, I am working on a project (board game in C#). positio I'm developing a  Solution 1 Unfortunately, in WinForms the Caret position is defined by the SelectionStart and SelectionLength properties, and you can't move it otherwise. So you can either select text, or set the caret to a specific position, but you can't do both. You can in WPF - there is the CaretIndex property - but it isn't available in WinForms.

TextBox In C, In this article, I will discuss how to create a TextBox control in and resize it using the mouse and set its properties and events. a Point that specifies the starting position of the TextBox on a Form. Styling In WPF About Us · Contact Us · Privacy Policy · Terms · Media Kit · Sitemap · Report a Bug · FAQ  Luis Garcia's Developer Story. Currently working as team leader of a group of 3 engineers, dedicated to support production (Assembly, EOL, robot arms and screwing stations) and developing continuous improvement projects (Andon, vision, cycle time reduction, among others) for high intensity LED boards (high/low beam lights), pump pressure, speed, and temperature sensor lines.

Pro C# 7: With .NET and .NET Core, You simply figure out the current location of the caret in the text box by using the If there is an error at said location (meaning the value is not null), you loop over WPF provides support for what might be considered control-agnostic events via and custom buttons), as well as keyboard shortcuts (e.g., Ctrl+C and Ctrl+V).

Comments
  • Unrelated but do you know that you can do: txtData.Text.Replace("$", "") which will replace all occurences of "$"? No need for that whole loop.
  • You might also want to look at the PreviewTextInput event.
  • Thanks Paul, yes I finally ended up using PreviewTextInpu where I avoid all the unnecessary characters.
  • Thanks LPL, I ended up using PreviewTextINput.
  • CaretIndex was useful indeed. Cheers
  • Sach prefers cursor on previous position, not at the end.
  • 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.
  • This doesn't work either. It for some reason moves the cursor to the next position after I hit Shift+$.