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.
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.