C#Forms TextBox选择带有插入位置的文本 [英] C# Forms TextBox select text with caret position

查看:107
本文介绍了C#Forms TextBox选择带有插入位置的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,



我无法弄清楚是否可以通过设置插入位置以编程方式选择TextBox中的文本?它始终将插入位置设置为选择结束:



http:/ /i.imgur.com/VS8rKsK.png [ ^ ]



我可以在选择开始时获得插入符号吗? (任何其他地方?)



http://i.imgur。 com / jiPz4dg.png [ ^ ]



谢谢!

Hey guys,

I can't figure out if it is possible to programmatically select text in TextBox with also setting caret position? It always sets caret position to the end of selection:

http://i.imgur.com/VS8rKsK.png[^]

Can I get caret at the beginning of selection? (any other place?)

http://i.imgur.com/jiPz4dg.png[^]

Thanks!

推荐答案

不幸的是,在WinForms中,Caret位置由SelectionStart和SelectionLength属性定义,而你否则就无法移动它。因此,您可以选择文本,也可以将插入符号设置为特定位置,但不能同时执行这两种操作。

您可以在WPF中使用CaretIndex属性 - 但它不适用于WinForms。

它可以完成,但你必须编写自己的文本框控件,这是很多工作!
Unfortunately, in WinForms the Caret position is defined by the SelectionStart and SelectionLength properties, and you can't move it otherwise. So you can either select text, or set the caret to a specific position, but you can't do both.
You can in WPF - there is the CaretIndex property - but it isn't available in WinForms.
It can be done but you'd have to write your own textbox control, which is a lot of work!


你说的不是真的。不,没有任何东西在选择结束时设置插入符号,并且没有任何内容设置插入符号。你的观察是不正确的。为了说清楚,选择一些文本就足够了。你会看到插入符在视觉上消失;它被选择替换了。通过选择文字来玩一下你会看到。



如果你愿意,你可以认为整个选定区域成为一个大插入符号。或者,如果需要,可以将插入符号视为零长度选择的渲染。这恰好与.NET API for TextBox 选择匹配: SelectionStart SelectionLength 。这样,从概念上讲,一些文本总是被选中。



-SA
What you say is not true. No, nothing sets a caret to the end of selection, and nothing sets a caret to the beginning of it. Your observation is incorrect. To make it clear, it's enough to select some text. You will see that the caret visually disappears; it is "replaced" by the selection. Play a bit with selecting text and you will see.

If you want, you can consider that the whole selected area becomes one big caret. Or, if you want, you can consider the caret as rendering of zero-length selection. This is precisely matched by the .NET API for TextBox selection: SelectionStart and SelectionLength. This way, conceptually, some text is "always selected".

—SA


这篇关于C#Forms TextBox选择带有插入位置的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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