如何找到一个光标在文本框中的位置? C# [英] How do I find the position of a cursor in a text box? C#

查看:107
本文介绍了如何找到一个光标在文本框中的位置? C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个标准的WinForms文本框,我想插在文本光标位置的文本。我怎样才能得到光标位置?

I have a standard WinForms TextBox and I want to insert text at the cursor's position in the text. How can I get the cursor's position?

感谢

推荐答案

不管是否选择任何文本,在<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.selectionstart.aspx\">SelectionStart产权重组presents索引到您插入记号位于文本。所以,你可以使用 String.Insert 注入一些文字,像这样:

Regardless of whether any text is selected, the SelectionStart property represents the index into the text where you caret sits. So you can use String.Insert to inject some text, like this:

myTextBox.Text = myTextBox.Text.Insert(myTextBox.SelectionStart, "Hello world");

这篇关于如何找到一个光标在文本框中的位置? C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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