Sizing a "RichTextBox"

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robert

    Sizing a "RichTextBox"

    Hi there,

    I have a dialog with a "RichTextBo x" 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(EventArg s e)
    {
    Graphics graphics = m_TextBox.Creat eGraphics();
    SizeF newTextBoxSize = graphics.Measur eString(m_TextB ox.Text,
    m_TextBox.Font, CalculateMaxTex tWidth());
    int newTextBoxWidth = (int)Math.Ceili ng(newTextBoxSi ze.Width);
    int newTextBoxHeigh t = (int)Math.Ceili ng(newTextBoxSi ze.Height);

    Width = Width + (newTextBoxWidt h - m_TextBox.Clien tSize.Width)
    Height = Height + (newTextBoxHeig ht - m_TextBox.Clien tSize.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.


Working...