I am trying to fit a WPF RichTextBox to exactly accommodate a grid of characters in a particular monospace font. I am currently using FormattedText to determine the width and height of my RichTextBox, but the measurements it is providing me with are too small--specifically two characters in width too small.
Is there a better way to perform this task? This does not seem to be an appropriate way to determine the size of my control.
RichTextBox rtb;
rtb = new RichTextBox();
FontFamily fontFamily = new FontFamily("Consolas");
double fontSize = 16;
char standardizationCharacter = 'X';
String standardizationLine = "";
for(long loop = 0; loop < columns; loop ++) {
standardizationLine += standardizationCharacter;
}
standardizationLine += Environment.NewLine;
String standardizationString = "";
for(long loop = 0; loop < rows; loop ++) {
standardizationString += standardizationLine;
}
Typeface typeface = new Typeface(fontFamily, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal);
FormattedText formattedText = new FormattedText(standardizationString, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, typeface, fontSize, Brushes.Black);
rtb.Width = formattedText.Width;
rtb.Height = formattedText.Height;
Assuming you have a very basic
RichTextBox
with just the standardFlowDocument
inside it then you have to take into account all of the extra layoutRichTextBox
uses.The
Document.PagePadding
property defaults to{5,0,5,0}
. So you will have to add 10 to the width.Additionally the
RichTextBox
has aBorderThickness
that defaults to{1, 1, 1, 1}
. So you will have to add 2 to the width.So the code you want could look like:
If you do that you will still be off by 1 character, but that's a bit misleading. The
RichTextBox
will wrap even if the text is just a tiny bit to wide not just a whole character width. If you add 2 to the width it works out. I can't figure out exactly where that extra 2 is coming from. I've never tried to get the width to be so exact. I have run into thePagePadding
andBorderThickness
issues before, but I just can't find the extra 2. It might just be a constant you are stuck with, but I doubt it. It's got to be somewhere.A small tip on producing the
StandardizationLine
you can doto clean up the loop.
I found a solution for the Rich text box height issues.. i have modified it a for general use..
Create following structs in your application....
Create following private variables in your class for form (where ever you need to calculate rich text height)
Add following method to your Class for form
You may need to modify above method to make it work as per your requirement... Make sure to send Rtf string as parameter to method not normal text and also make sure to assign available width and height to the Richtextbox variable in the method...
You can play with WordWrap depending on your requirement...