How can Control/Form know if it's being displayed during design or in runtime?

T

TerryStone

I have created a control that displays a list of items. During design
mode I fill it with junk data using calls from the constructor. So
when I look at a form with the control on, instead of being empty, it
has some content, and this gives a better idea of what the form will
look like when run. During runtime it should be empty to start with,
so I comment out the code in the constructor that fills it with dummy
data.

How can I code the control/form so that the constructor knows it is
being shown in design mode?

Thanks in advance for any replies.

(My appologies for use of the term "know" in relation to a software
program.)
 
E

Eric

TerryStone said:
How can I code the control/form so that the constructor knows it is
being shown in design mode?

maybe this will help:

http://msdn2.microsoft.com/en-us/library/35ea88wb.aspx

or this:

http://msdn.microsoft.com/msdnmag/issues/03/12/CuttingEdge/

To quote Dino: "The IComponent interface has just one property, Site,
which corresponds to an object that implements the ISite interface. The
ISite interface defines four properties: Component, Container,
DesignMode, and Name".

if (this.Site != null && this.Site.DesignMode)
{
string buf = "'{0}' is hosted by {1}";
MessageBox.Show(String.Format(buf, this.Site.Name,
this.Site.Container.ToString()));
}
else
MessageBox.Show("The control works in a run-time context.");

Eric
 
B

Bruce Wood

TerryStone said:
I have created a control that displays a list of items. During design
mode I fill it with junk data using calls from the constructor. So
when I look at a form with the control on, instead of being empty, it
has some content, and this gives a better idea of what the form will
look like when run. During runtime it should be empty to start with,
so I comment out the code in the constructor that fills it with dummy
data.

How can I code the control/form so that the constructor knows it is
being shown in design mode?

You can't.

There is a DesignMode property that tells you whether you are in design
mode, but it is correct only after your control / form has a window
handle, which happens well after it is constructed.

You need to move your dummy-data populating code from your constructor
into an OnLoad routine / Load event handler. In there, the DesignMode
property will return the correct result, and you can suppress
populating your form when not in design mode.
 

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