Control.DesignMode and OnPaint in Custom Control

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
 
C

Claes Bergefall

I don't really have an answer to your problem but there is something you
should know about the DesignMode property. Try this:
1. Put your control on a UserControl
2. Put the UserControl on a Form
3. Open the Form in the designer
Your control is now in runmode, i.e. the DesignMode property is false!
What you need is a DesignMode property that also checks all its parents.
You can find it here:
http://groups.google.se/[email protected]&lr=&hl=sv

Perhaps it will also fix the problem you're having when removing the control

/claes


Dave Humphrey said:
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 action
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?
 
G

Guest

Hi

Thanks for this. It will likely fix my problem. I'll have to read more deeply into how Control is implemented to understand why this is true

Dave
 

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