How to track which character is deleted in TextBox in WPF?

前端 未结 3 961
闹比i
闹比i 2021-01-05 07:19

I want to track which character is deleted by the user through Delete or BackSpace Key.

I am handling TextBox_ChangedEvent of textbox.

Can I extract the dele

3条回答
  •  别那么骄傲
    2021-01-05 07:50

    I don't know WPF but assuming that it's same as WinForms for this (seems probable). The only way I know of is that you actually keep the current text in a variable and on text change, if it's not a delete or backspace, you update that text, otherwise you use it compare what's changed and if that change should be allowed.

    Edit: Looking at TextChangedEventArgs.Changes it seems like the way I describe above might still be the way to go, but that you maybe could use the Changes to compare the texts more efficiently.

    You might already have thought about it, but otherwise, remember to handle cut and paste also (and that the user might be doing that with the mouse rather than the keyboard).

提交回复
热议问题