是否知道Winforms TextBox中插入符号的点位置? [英] Knowing the point location of the caret in a Winforms TextBox?
问题描述
我需要知道插入符号当前所在的窗口的确切位置,以便可以在文本下方弹出一个小窗口(类似于智能感知或拼写检查器).问题是GetPositionFromCharIndex似乎不适用于TextBoxes.这就是我想要做的:
I need to know the exact point location in my window where the caret currently resides so that I can pop up a small window under the text (similar to intellisense or a spellchecker). The issue is that GetPositionFromCharIndex doesn't seem to work on TextBoxes. This is what I'm trying to do:
Point pt = mTextBox.GetPositionFromCharIndex(mTextBox.SelectionStart);
pt.Y = (int)Math.Ceiling(mTextBox.Font.GetHeight());
mListBox.Location = pt;
但是,GetPositionFromCharIndex始终为TextBoxes返回(0,0)(显然,它没有像RichTextBox那样实现).虽然此功能(和此代码)对于RichTextBoxes很好用,但我不想将其余代码从TextBox迁移到RichTextBox,除非是绝对的 LAST 方法.与RichTextBox/TextBox的兼容性很多,我必须重写我的代码中的很多大块来实现.
However, GetPositionFromCharIndex always returns (0, 0) for TextBoxes (apparently it isn't implemented like it is for RichTextBox). While this function (and this code) works fine for RichTextBoxes, I don't want to migrate the rest of my code from TextBox to RichTextBox except as an absolute LAST resort. There are so many incompatibilities with RichTextBox/TextBox that I'll have to rewrite a very large chunk of my code to do this.
有没有知道如何执行此操作的Winforms向导?
Are there any Winforms wizards out there who know how to do this?
推荐答案
If you don't mind using Win32-API use GetCaretPos to get the exact position.
这篇关于是否知道Winforms TextBox中插入符号的点位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!