2

I created a user control using C# for windows form application. This user control has some properties. In runtime, if the user does not enter values for this properties I want to show a message box and exit the application.

The problem is when I write the checking code in the Load event of User Control. When I drag & drop it on the form the message box will appear.

private void UserControl1_Load(Object sender, EventArgs e) 
{
    if (_getFirstPageArgument==null || _getFirstPageArgument.Length==0) 
    { 
        throw new Exception("Some Message"); 
    }
}

How do I distinguish between load on the form and load on run time?

Justin
  • 6,611
  • 3
  • 36
  • 57
Arian
  • 12,793
  • 66
  • 176
  • 300

3 Answers3

5

I fear there is a larger problem here. But to solve your immediate problem (if I understand correctly...) There is a form attribute called DesignMode. When you are in the visual studio design mode, this will be true. At runtime, this will be false.

Mike Park
  • 10,845
  • 2
  • 34
  • 50
  • @climbage: I can't find DesignMode.I write this code Form frmParent = this.ParentForm; but frmParent Does not have DesignMode – Arian Mar 23 '11 at 20:03
  • +1 this is the solution. It's common to see "if (!this.DesignMode) return;" as the first line of a Load event handler. – Joe Mar 23 '11 at 20:03
  • @Nima - DesignMode is a protected property, you need to use this.DesignMode instead. – Joe Mar 23 '11 at 20:06
  • @Joe: thanks but problem still remain.I explain it as answer below. – Arian Mar 23 '11 at 21:04
  • Thanks my friend.yes it's not quiet relible.Consider this scenario: I create a user control and place it on a form.my user control has a property of type obejct[]. I want it's not null or empty in runtime and assign this peroperty at form load event.the new problem is load event of user control execute before form load event and however DesignMode is false but because my property not assign yet message boxes appear – Arian Mar 23 '11 at 21:03
  • Ok so I'm trying to wrap my head around what you're trying to accomplish. You have a form with a user control in it. When your form loads you want to set a value in the user control... and when your user control loads you want to check that value and then display the message? – Mike Park Mar 23 '11 at 21:09
  • Ok I gotcha. Does this value check have to run `OnLoad`? or can you have it run off of some other event that is guaranteed to execute after the form has completely loaded and the value is set? – Mike Park Mar 23 '11 at 21:18
  • @climbage: load event not matter.I want to check values beform form shows. – Arian Mar 23 '11 at 21:21
1

For beginners, @Nimas case can be a good study point to understand that Visual Studio actually runs and executes parts of our code even when we are in design time, which is why the constructor is invoked. Even "DesignMode" property is not 100% reliable. You can find an interesting note here related to that http://weblogs.asp.net/fmarguerie/archive/2005/03/23/395658.aspx

James Poulose
  • 3,569
  • 2
  • 34
  • 39
0

If you only want to know when the type itself has been loaded into the runtime (not a specific instance), you can put code into the static constructor for that class.

If I'm misinterpreting your question, please clarify using a timeline when you want specific events to happen.

Tejs
  • 40,736
  • 10
  • 68
  • 86