I am using GetScrollRange to set a "preferred width" for a UserControl that
contains a RichTextBox. I think this should return the smallest width such
that no horizontal scroll will be needed. Most of the time this seems to
work, but sometimes it returns an incorrect (I'm guessing out of date)
value. I notice that sometimes after I set the RichText1.Rtf value, the
prefered width seems to be the value it should have been before changing the
..Rtf. Other times, it just seems to be a little too narrow.
Anyone know if there is a way I can force it to get up-to-date information,
or if there is another more accurate way to do this?
----
[ DllImport( "user32", CharSet=CharSet.Auto ) ]
public static extern bool GetScrollRange( IntPtr hWnd, int nBar,
out int lpMinPos, out
int lpMaxPos );
public int PreferredWidth
{
get
{
const int MINWIDTH = 600;
const int SB_HORZ = 0;
int Min, Max;
GetScrollRange( RichText1.Handle, SB_HORZ, out Min, out Max );
// assume vertical scrollbar for now
int Width = Max - Min +
2 * SystemInformation.Border3DSize.Width +
SystemInformation.VerticalScrollBarWidth;
return Math.Max( MINWIDTH, Width );
}
}
----