Sizing a "RichTextBox"

R

Robert

Hi there,

I have a dialog with a "RichTextBox" that's anchored on all 4 sides of the
dialog. I now want to resize this control in my "OnLoad()" handler so that
it reflects the size of its text. Because of the anchoring, I can do this by
simply resizing the dialog itself. The (condensed) code basically looks like
this:

class MyDialog : Form
{
private RichTextBox m_TextBox;

protected override void OnLoad(EventArgs e)
{
Graphics graphics = m_TextBox.CreateGraphics();
SizeF newTextBoxSize = graphics.MeasureString(m_TextBox.Text,
m_TextBox.Font, CalculateMaxTextWidth());
int newTextBoxWidth = (int)Math.Ceiling(newTextBoxSize.Width);
int newTextBoxHeight = (int)Math.Ceiling(newTextBoxSize.Height);

Width = Width + (newTextBoxWidth - m_TextBox.ClientSize.Width)
Height = Height + (newTextBoxHeight - m_TextBox.ClientSize.Height);
}
}

This works except that the vertical scrollbar now shows up sometimes in
"m_TextBox". If I increase "Height" on the last line by just a few extra
pixels however (sometimes just one), then the vertical scroll bar
disappears. I can't play guessing games however so does anyone know how to
calculate the "Height" so that it always accomodates.the control without a
scrollbar. Thanks.
 
C

ClayB

Maybe you need to add either

2 * SystemInformation.BorderSize
or
2 * SystemInformation.Border3DSize

to account for the border at the top and bottom.

===============
Clay Burch
Syncfusion, Inc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top