如何定位查找对话框以不覆盖找到的文本?

发布于 2024-09-14 22:57:30 字数 830 浏览 1 评论 0原文

我创建了一个用于在 RichTextBox 中搜索的无模式查找对话框,但在选择找到的文本后定位查找对话框时遇到问题,因此它不会覆盖所选文本。我尝试使用以下命令获取相对于客户区域的行号:

this.lineCount = this.rtb.Height / (this.rtb.Font.Height+2);

rtb.Select(rtbIndex, searchText.Length);

int linePos = (this.rtb.GetLineFromCharIndex(this.rtb.GetFirstCharIndexOfCurrentLine())) % this.lineCount;

if(linePos<(this.lineCount/2))
{
    this.Location = rtb.PointToScreen(new Point(rtb.Bounds.Left, rtb.Bounds.Bottom - this.Height));
}
else
{
    this.Location = rtb.PointToScreen(new Point(rtb.Bounds.Left, rtb.Bounds.Top));
}

this.lineCount 是根据字体高度和 Richtextbox 的高度适合客户区域的行数。这是我已经验证过的准确值。如果 lineNum 小于 this.lineCount 值的一半,我的代码会将查找对话框定位在 richtextbox 的底部,否则在顶部

Hoewever,linePos 不可靠。当包含所选文本的行是第 19 行且 lineCount 为 20 时,它的值有时为零,因此对话框会移到所选文本上。因此,它无法可靠地计算 Richtextbox 在何处显示所选文本。

I created a modeless find dialog for use in searching in a RichTextBox and am having trouble positioning the find dialog after the found text is selected so that it does not cover the selected text. I have tried to get the line number relative to the client area using the following:

this.lineCount = this.rtb.Height / (this.rtb.Font.Height+2);

rtb.Select(rtbIndex, searchText.Length);

int linePos = (this.rtb.GetLineFromCharIndex(this.rtb.GetFirstCharIndexOfCurrentLine())) % this.lineCount;

if(linePos<(this.lineCount/2))
{
    this.Location = rtb.PointToScreen(new Point(rtb.Bounds.Left, rtb.Bounds.Bottom - this.Height));
}
else
{
    this.Location = rtb.PointToScreen(new Point(rtb.Bounds.Left, rtb.Bounds.Top));
}

this.lineCount is the number of lines that fit in the client area based on the font height and the height of the richtextbox. It is an accurate value that I have verified. My code positions the find dialog at the bottom of the richtextbox if lineNum is less than half the value of this.lineCount, otherwise at the top

Hoewever, linePos is not reliable. It sometimes has a value of zero when the line with the selected text is the 19th line and the lineCount is 20, so the dialog gets moved over the selected text. Thus it does not reliably calcualte where the richtextbox displays the selected text.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人事已非 2024-09-21 22:57:30

您不需要自己计算字符位置,您可以使用 GetPositionFromCharIndex 方法

You don't need to calculate the character position yourself, you can get it using the GetPositionFromCharIndex method

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文