Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
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!

VipHaLong
Posted
Comments
BillWoodruff 27-Dec-22 0:50am    
"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"

This makes no sense.

Assume a line of text in an RTF control where different characters are in varying fonts, styles, and sizes: what is your goal ? What happens at run-time ?


p.s. I spent years dealing with issues of character size and position, putting text on complex paths, etc., at companies like Adobe.

1 solution

Hi,
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.
C#
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.

Regards
Jegan
 
Share this answer
 
v2
Comments
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.

Jegan

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900