如何在插入标记位于所选内容的开始位置的文本框中选择文本? [英] How to select text in a TextBox with the caret positioned at the START of the selection?

查看:51
本文介绍了如何在插入标记位于所选内容的开始位置的文本框中选择文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 System.Windows.Forms.TextBox .可以使用键盘来选择文本,以使插入符号位于所选内容的开始处-通过按住Shift键并将插入符号向左移动.

I'm using a System.Windows.Forms.TextBox. It is possible to select text by using the keyboard in such a way that the caret is positioned at the start of the selection - by holding Shift and moving the caret to the left.

我想通过编程来做同样的事情.

I would like to do the same programmatically.

例如,假设我有一个文本框为"Some sample text".我希望选择样本"一词,并且将插入符号定位在样本"中的"s"之前.

For example, suppose I have a text box with the text "Some sample text". I would like the "sample" word to be selected, and the caret to be positioned just before the "s" in "sample".

如果我这样做:

textbox.SelectionStart = 5;
textbox.SelectionLength = 6;

然后我选择了单词,但插入符号在样本"中的"e"之后.

then I get the word selected, but the caret is just after "e" in "sample".

如果我这样做:

textbox.SelectionStart = 11;
textbox.SelectionLength = -6;

我有例外.

如何将插入符号放置在选择的开始位置?

How can I position the caret at the start of the selection?

推荐答案

我无法编程.如 MSDN 本机Windows控件不管开始和结束的相对值如何,在结束位置都会显示闪烁的插入符号.

I don't that is possible to program. As it is stated in MSDN native windows controls displays a flashing caret at the end position regardless of the relative values of start and end.

这篇关于如何在插入标记位于所选内容的开始位置的文本框中选择文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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