简体   繁体   English

WPF(使用C#)TextBox光标位置问题

[英]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. 我有一个WPF C#程序,尝试在TextChanged事件中从文本框中删除某些字符。 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. 我遇到的问题是,每当用户输入$符号(Shift + 4)时,在TextChanged事件中,它都会从文本框文本中删除$字符,但是还会将光标移动到不是我的文本框的BEGINNING所需的功能。

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. 假设例如文本框中的文本为123ABC,如果我将光标放在3后面,那么将光标移到文本的末尾将意味着在下一个击键时,用户将在C之后而不是3之后输入数据。正常功能。

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? 如何定义TextBox输入限制?

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 . 如果对您来说e.Handled = true为您要在PreviewKeyDown (使用SHIFT键的Keyboard.Modifiers )或PreviewTextInput避免的所有字符设置e.Handled = true

Try TextBox.CaretIndex for restoring cursor position in TextChanged event. 尝试使用TextBox.CaretIndex来恢复TextChanged事件中的光标位置。

Hope it helps. 希望能帮助到你。

You can use the Select function of TextBox to change the cursor position. 您可以使用TextBox的Select功能来更改光标位置。

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 您可以了解有关在MSDN上定位光标的更多信息

You can try Regular Expression Sample 您可以尝试正则表达式示例

1) Use PreviewTextInput="CursorIssueHandler" in .xaml file 1)在.xaml文件中使用PreviewTextInput="CursorIssueHandler"

2) In your .cs file ,write the below code: 2)在您的.cs文件中,编写以下代码:

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

You can use the SelectionStart property of the textbox. 您可以使用文本框的SelectionStart属性。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM