替换文本框末端的数字后,光标位置指向文本框的开头 [英] The cursor position is pointing at the beginning of the text box after replacing digits at theend of the textbox

查看:78
本文介绍了替换文本框末端的数字后,光标位置指向文本框的开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



i有一个文本框,可以接受数字和小数值。

目前正在使用替换方法,例如



Hi,
i have an textbox which accepts numeric and decimal values.
currently am uisng replace method such as

if (txtPlannedHours.Text.Length>1 && 
     !(String.IsNullOrWhiteSpace(txtPlannedHours.SelectedText)))
{
  txtPlannedHours.Text = 
     txtPlannedHours.Text.Replace(txtPlannedHours.SelectedText, e.Text);
}





现在如果我的值为126,如果我只选择26并尝试替换为8,文本框显示为818而不是18.它在文本框的开头和结尾附加一个替换值,并且始终在文本框的开头显示光标的位置。



任何人都可以为我提供解决方案吗?



我在活动中添加上述代码。

我正在使用带有WPF的C#。



So now if i have a value as 126 and if i select only 26 and try to replace with 8, the textbox is displaying as 818 instead of 18. It is appending a replaced value at the beginning and the end of the textbox and always the position of the cursor is being displayed at the beginning of the textbox.

Can anyone provide me a solutio for this?

Am adding the above code in an event.
Am using C# with WPF.

推荐答案

WPF事件PreviewTextInput特定于WPF的所有控件的实现。我无法在WinForms中复制你在WPF中观察到的行为。



经过一番思考,我得出结论你有两个问题:



1.替换操作的意外副作用



2.未能观察到TextBox中插入光标位置的预期变化。



我假设问题2可以解释为 TextBox Control必须具有焦点才能执行您的操作。



问题#1更令人费解。但是,评估这两个问题是否与TextBox没有焦点相关的良好的第一步是你只需要重新定位设置Focus到之前的TextBox
The WPF Event PreviewTextInput is specific to WPF's implementation of all Controls. I cannot replicate the behavior you observe in WPF in WinForms.

After some thought, I conclude that you are having two problems:

1. unexpected side-effects of the replace operation

2. failure to observe expected change in the position of the insertion cursor in the TextBox.

Issue #2 can, I hypothesize, be explained by the fact that the TextBox Control must have Focus before the operations you perform.

Issue #1 is more puzzling. But, a good first step in evaluating whether both issues might be related to the TextBox not having Focus is for you simply to re-locate setting Focus to the TextBox before you operate on it:
txtPlannedHours.Focus();

txtPlannedHours.Text = txtPlannedHours.Text.Replace(txtPlannedHours.SelectedText, e.Text);

txtPlannedHours.SelectionStart = txtPlannedHours.Text.Length;

现在,如果这个改变没有区别,那么下一个逻辑假设就是你在特定的WPF事件中有什么东西处理这会影响结果。



因此,通过使用断点或写入控制台仔细观察......变得更加重要... 'SelectedText和e.Text的值是在执行更改TextBox内容的代码之前和之后的值。



我建议你尝试各种选择,并查看前后值,看看是否看到模式。考虑在这里发布这些观察结果。

Now, if this change makes no difference, the next logical hypothesis is that there is something in the specific WPF Event you are handling that's affecting the outcome here.

So, it becomes even more critical to carefully observe ... by using break-points, or writing to the Console ... what the values of both the 'SelectedText and the value of e.Text is before and after you execute your code that changes the contents of the TextBox.

I suggest you try a variety of selections, and look at the before-and-after values, and see if you see a pattern. Consider posting those observations here.


这篇关于替换文本框末端的数字后,光标位置指向文本框的开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆