G
Guest
I posted this a while back to the Drawing forum, but got no answer (thanks to those who helped with debugging though). I'm wondering if someone else can take a crack at it
My situtation is this: I have written a custom WinForms control (inherits from ScrollableControl). In my overriden OnPaint (included below) I do a check to see if the control is in DesignMode or not. Since the control can only be drawn at runtime (images are fed from a network connection), I draw a static graphic at DesignTime to give the programmer something to look at :
protected override void OnPaint(PaintEventArgs pe
base.OnPaint(pe)
Graphics g = pe.Graphics
// If the control is in design mode, draw a nice background, otherwise paint the desktop
if (!DesignMode)
System.Diagnostics.Debug.Assert(desktop != null)
g.DrawImage(desktop, DisplayRectangle);
} else
// Draw a static screenshot of a Windows desktop to simulate the control in actio
g.DrawImage(designModeDesktop, DisplayRectangle)
In my class' constructor I load an embedded image from the assembly and cache it in designModeDesktop. The desktop image is only created at run time, and is dynamically drawn in real time based on data received over the network
When I drag-and-drop the control from the Toolbox onto a host form, it works fine, and the designModeDesktop image appears as expected. However, when I click the control and then Delete, I get a NullReferenceException. Debugging has shown that the reason is that it is now trying to paint the desktop image instead of designModeDesktop--something that is only possible if the control is no longer in DesignMode!
Is there something about the Control.DesignMode property that I don't understand? I am assuming that it will be true so long as the control is not being run. Why is DesignMode false when the control is being removed from the form at Desgin time? Is there a short period before Control.Dispose that is not DesignMode, but also not "Runtime Mode?" If so, how do I catch the case that this is true without doing a hack to test for a null desktop variable
Dave
My situtation is this: I have written a custom WinForms control (inherits from ScrollableControl). In my overriden OnPaint (included below) I do a check to see if the control is in DesignMode or not. Since the control can only be drawn at runtime (images are fed from a network connection), I draw a static graphic at DesignTime to give the programmer something to look at :
protected override void OnPaint(PaintEventArgs pe
base.OnPaint(pe)
Graphics g = pe.Graphics
// If the control is in design mode, draw a nice background, otherwise paint the desktop
if (!DesignMode)
System.Diagnostics.Debug.Assert(desktop != null)
g.DrawImage(desktop, DisplayRectangle);
} else
// Draw a static screenshot of a Windows desktop to simulate the control in actio
g.DrawImage(designModeDesktop, DisplayRectangle)
In my class' constructor I load an embedded image from the assembly and cache it in designModeDesktop. The desktop image is only created at run time, and is dynamically drawn in real time based on data received over the network
When I drag-and-drop the control from the Toolbox onto a host form, it works fine, and the designModeDesktop image appears as expected. However, when I click the control and then Delete, I get a NullReferenceException. Debugging has shown that the reason is that it is now trying to paint the desktop image instead of designModeDesktop--something that is only possible if the control is no longer in DesignMode!
Is there something about the Control.DesignMode property that I don't understand? I am assuming that it will be true so long as the control is not being run. Why is DesignMode false when the control is being removed from the form at Desgin time? Is there a short period before Control.Dispose that is not DesignMode, but also not "Runtime Mode?" If so, how do I catch the case that this is true without doing a hack to test for a null desktop variable
Dave