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.