如何用C#中的新文本替换文本框中的选定文本 [英] How to replace a selected text in a text box with a new text in C#

查看:81
本文介绍了如何用C#中的新文本替换文本框中的选定文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个接受小时的文本框。

所以现在当我选择全部或部分文本时,我应该可以替换。





所以我怎样才能在C#中达到这个条件?





尝试使用以下条件

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





格式是否正确?

Hi,
I have a textbox which accepts hours.
so now when i select the entire or part of the text , i should be able to replace.


so how can i achieve this condition in C#?


Am trying with the below condition
txtPlannedHours.Text = txtPlannedHours.SelectedText.Replace(txtPlannedHours.Text,"");


is it correct format?

推荐答案

应该是这样的:

Should be like this:
txtPlannedHours.Text = txtPlannedHours.Text.Replace(txtPlannedHours.SelectedText,"");


您只需要:
textBox1.Text = textBox1.SelectedText;

TextBox Win Form中的控制在失去焦点时保留任何选择;但是,如果您认为存在以某种方式丢失选择的危险,您可以始终定义TextChanged EventHandler,并使用当前的SelectedText更新变量。

The TextBox Control in Win Forms retains any selection when it loses focus; however, if you think there's a danger of somehow losing the selection, you could always define a TextChanged EventHandler, and update a variable with the current SelectedText.


这篇关于如何用C#中的新文本替换文本框中的选定文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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