I'm using Control.PreferredSize in order to determine what the ScrollableControl.AutoScrollMinSize should be for a Form. This will need to be set whenever the control's PreferredSize property changes, but there doesn't appear to be a Control.PreferredSizeChanged event. Is there a way to detect when this property changes (possibly using Control.WndProc)? I would prefer to avoid polling the property if it can be avoided.
+1
A:
You can override OnLayout or OnPaint.
private Size m_CurrentPreferedSize;
protected override void OnLayout(LayoutEventArgs e)
{
base.OnLayout(e);
Size newSize = PreferredSize;
if(m_CurrentPreferedSize != newSize)
{
m_CurrentPreferedSize = newSize;
//Your code here
}
}
PreferredSize is calculated on every call.
Alex
2010-08-19 19:36:34
I'd rather not override OnPaint because that method is called so often. It's not clear that OnLayout is *guaranteed* to be called every time PreferredSize changes, but it does seem to work; I was unable to find a scenario that caused PreferredSize to change that did not also cause OnLayout to be called.
CodeSavvyGeek
2010-08-25 01:40:01