2

I am creating an extended RichTextBox control, where I am drawing icons next to the lines of text. I am using GetPositionFromCharIndex to find the current line and next line Y coordinate, so that the height and middle of the current line can be determined. This method obviously stops working when the last line is encountered because there is no next line. Is there some way to determine the height of the last line of text? Note, the font size can vary from line to line.

1 Answer 1

3

Not easy to do. Maybe there is an API call for this, but I'm not 100% sure.

A hacky way to accomplish this is to use an off-screen RichTextBox control and transfer the RTF property to it, which just happens to append an extra \par at the end for you, so there is an extra line now:

using (RichTextBox r = new RichTextBox()) { r.SelectAll(); r.SelectedRtf = richTextBox1.Rtf; for (int i = 1; i < r.Lines.Length; i++) { Point p1 = r.GetPositionFromCharIndex(r.GetFirstCharIndexFromLine(i - 1)); Point p2= r.GetPositionFromCharIndex(r.GetFirstCharIndexFromLine(i)); int height = p2.Y - p1.Y; MessageBox.Show(String.Format("Line #{0} height = {1}", i - 1, height)); } } 
Sign up to request clarification or add additional context in comments.

1 Comment

I had a similar idea, but it involved adding a newline at the end of the actual text.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.