Richtextbox C#中的插入符号坐标 [英] Coordinates of Caret in richtextbox C#

查看:229
本文介绍了Richtextbox C#中的插入符号坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好.我必须在richtextbox C#中获得插入符号的坐标,但是我不知道该如何获得它!我想要获取它的x坐标和y坐标.
希望您能帮帮我!
非常感谢!

Hello. I have to get Coordinates of Caret in richtextbox C# but I don''t know how to get it! I want get the x coordinate and the y coordinate of it.
I hope you can Help me!
Thank you very much!

推荐答案

Toli建议,也许,如果您使用的是Google,那么您可能已经找到了解决方案.

也许是一个复杂的Google:"Richtextbox插入符号位置"可能会有所帮助.

它将其放在列表的顶部: http://msdn.microsoft.com/zh-cn/library/system.windows.controls.richtextbox.caretposition(v = vs.90).aspx [
Toli was suggesting that perhaps, if you used Google, you might have found the solution to this a while ago.

Perhaps a complicated google: "Richtextbox caret position" might help.

It put this at the top of the list: http://msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox.caretposition(v=vs.90).aspx[^] when I tried it...

Next time, don''t be so lazy!


Griff的答案是正确的,但仅适用于WPF.

对于System.Windows.Forms,则比较棘手.文本中的位置由System.Windows.Forms.RichTextBox.SelectionStart定义.这是一个字符索引.您可能需要一个行号和行中的位置.为此,首先使用GetLineFromCharIndex;它会给你线.当您知道该行时,请使用GetFirstCharIndexFromLine再次获取字符索引.原始字符索引与行首字符索引之间的差异将计算行(即列)中的位置.有关更多信息,请参见其他System.Windows.Forms.RichTextBox.Get...方法.

除了Griff的答案:对于WPF,CaretPosition将返回TextPointer的实例.使用此类可以找出直线和直线位置.

—SA
Griff''s Answer is correct, but only for WPF.

For System.Windows.Forms it is more tricky. The position in text is defined by System.Windows.Forms.RichTextBox.SelectionStart. This is a character index. You probably need a line number and position in line. For this purpose, first use GetLineFromCharIndex; it will give you the line. When you know the line, use GetFirstCharIndexFromLine to get the character index again. The difference between your original character index and the character index at the beginning of the line will calculate the position in the line, that is, a column. See other System.Windows.Forms.RichTextBox.Get... methods for more information.

In addition to Griff''s Answer: for WPF, CaretPosition will return an instance of TextPointer. Use this class to find out the line and position in line.

—SA


这篇关于Richtextbox C#中的插入符号坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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