问题描述
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.
推荐答案
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.
这篇关于替换文本框末端的数字后,光标位置指向文本框的开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!