如何定位查找对话框以不覆盖找到的文本?
我创建了一个用于在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要自己计算字符位置,您可以使用
GetPositionFromCharIndex
方法You don't need to calculate the character position yourself, you can get it using the
GetPositionFromCharIndex
method