Set RichTextBox width based on contents

R

Rachel Suddeth

I would like to load a file to the .Rtf property of a RichTextBox, and then
set the containing form (if possible, depending on screen resolution) to the
smallest width that will not cause the horizontal scrollbar to appear.

Now the RichTextBox must know what width will cause the horizontal scrollbar
to disappear because, after all, the scrollbar does disappear if you make it
wide enough (the RichTextBox is docked to the form.) Any idea how my code
can get that information? I have actually tried to calculate it by grabbing
the Graphics object of the RichTextBox and using MeasureString() on the
longest line, but that did not give the right answer, plus it should not be
necessary... the control has to have this information already I would think.
(If it were a ScrollableControl, it would be AutoScrollMinSize.Width, but
it's not...)

-Rachel
 
R

Rachel Suddeth

Rachel Suddeth said:
I would like to load a file to the .Rtf property of a RichTextBox, and then
set the containing form (if possible, depending on screen resolution) to the
smallest width that will not cause the horizontal scrollbar to appear.
Here's a thought...
Would it work if I used interop with GetScrollRange? Like this:
----
[ DllImport( "user32", CharSet=CharSet.Auto ) ]
public static extern bool GetScrollRange( IntPtr hWnd, int nBar,
out int lpMinPos, out int lpMaxPos );

....


GetScrollRange( richTextBox1.Handle, SB_HORZ, out min, out max );
---
Would the "max" value for the horizontal ScrollRange give me the width I'd
need to avoid scrolling? I'm not familiar with the function, this is purely
guessing. And if it did, would it matter if the form that displayed the
control was loaded first? And what would happen if it was already wide
enough and the scrollbar wouldn't appear anyway? Anyone know?

-Rachel
 

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