Click here to Skip to main content
13,049,814 members (72,364 online)
Rate this:
Please Sign up or sign in to vote.
See more:
The method GetPositionFromCharIndex(int) returns only the position of a specified char at a specified index in RichTextBox, in some case, it's not enough, I want to get the bound or Rectangle of a specified char (at a specified index) in RictTextBox. (the position obtained from GetPositionFromCharIndex() is exactly the Location of the Rectangle I want). That means the problem can be solved if we know the Size of that Rectangle.

I find difficult to find the Height of that Size structure. (the width is OK). Please notice that that Height is not the font height of the character. If you have a letter A with fontsize being 14 and a letter B right follows A but has a fontsize of 30. The Height here for both these letters should be the same (however the Width can be different, that means the Sizes of the both are also different).

Searching for the maximum Height of letters in the same line is an approach but I think it is really hard and not efficient. I'm finding another solution better.

Please help me solve this. It's a little urgent. Thanks!

Posted 9-Mar-13 12:18pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

you will have to create the graphics for the rich text control, parse the text you wanted to parse and get the size of that text.
var font = new System.Drawing.Font(this.Font, FontStyle.Regular);
Graphics graphics = richTextBox1.CreateGraphics();
SizeF textSize = graphics.MeasureString("A", font);
float width = textSize.Width;

This will be the width of the text you are parsing, if you parse a word, it would be the width of the word and so on.

You must bear in mind that this single Font parameters into account, if the text is mixture of Font parameters then it should be added in a loop of fonts and corresponding texts.

supernorb 10-Mar-13 15:22pm
Please read my question carefully, I said that getting the Width is OK, I didn't say how to do, but actually the way I used is the solution you suggested here, but that's the Width, the Height is different, the Height of SizeF is the real height of the character in the specified font, but I don't want its real height, please read my question again (the example about a line of 2 letters A and B with different font sizes). You can imagine that in a line, we have many letters with different font sizes, what I want is the maximum height of charaters in that line, all the characters in a line should have the same Height (the Height of each character's rectangle/bounds). Looping through each character in a line is not easy, because a line here is not real line (ends with a return character, it is just wrapped) and the performance of looping is really bad. Anyway thank you for the reply!
Jegan Thiyagesan 10-Mar-13 18:16pm
As you have not posted any code fragments in your code, I had to assume that your coding knowledge is limited.

The RichText control inherits from "Block Class", if you read about Block Class, you will know how line height works.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 10 Mar 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100