Design time problems

  • Thread starter Thread starter Chris Capel
  • Start date Start date
C

Chris Capel

Is there any boolean flag set during design time that a UserControl can test
to see whether to run code that only works during runtime (because of
complicated initialization)?

Like this:

public class MyControl : UserControl {
public MyControl() {
InitializeComponent();
if (!System.ComponentModel.Design.MagicClass.IsDesignTime)
DoSomethingUnsafeAtDesignTime();
}
}

Otherwise, I can't put my control on my form with the designer, which is
mighty annoyin'.

Chris
 
The UserControl has a Boolean property called DesignMode you can check.

Strangely enough, this doesn't work at all. Example:

using System;
using System.Windows.Forms;

namespace Something {
public class MyUserControl : System.Windows.Forms.UserControl {
public MyUserControl() {
if (DesignMode)
MessageBox.Show("Hi, I'm in design mode.");
else
MessageBox.Show("Sorry, but no design mode :-(");
}
}
}

If you place this in a code file in a project, load the designer for it (to
register it with the design-time environment) and then try to drag it on a
form, it shows a message box saying "Sorry, but not design mode :-(".
Strange.

Chris
 
Hi Chris,

It appears that the property dosen't get set until after the constructor
completes. The container (in this case the form) has to interact with the
control's ISite interface and I'm not sure at what point of the control
construction phase that happens. Anyway, if you put your if statement in the
contol's Load event it will work properly.
 
Back
Top