views:

68

answers:

1

Why are messages displayed at design-time?

My code is :

class Class1 : TextBox
{
    public Class1()
    {
        this.Resize += new EventHandler(Class1_Resize);
    }

    void Class1_Resize(object sender, EventArgs e)
    {
        MessageBox.Show("Resize");
    }
}

Pic :

alt text

+4  A: 

Because that's the way the Form designer works. It's actually instantiating your control when it displays it in your form at design-time. Thus when you resize the control in the designer, your code for the message box fires.

Kirk Woll
+1. Checking with `if (!DesignMode)` should help resolve the OP's issue.
Ani
Checking `(!DesignMode)` in constructor won't help (since object is not created yet, designer cannot set DesignMode = true).
max
@max: Good point; either conditionally registering the handler on `OnLoad`, or conditionally suppressing the action in the handler itself is the way to go.
Ani