【问题标题】:Winforms: Why do events fire at design-time?Winforms:为什么事件会在设计时触发?
【发布时间】:2010-10-10 16:55:43
【问题描述】:

为什么在设计时显示消息?

我的代码是:

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

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

图片:

【问题讨论】:

    标签: c# .net winforms events windows-forms-designer


    【解决方案1】:

    因为这就是表单设计器的工作方式。当它在设计时在表单中显示它时,它实际上是在实例化您的控件。因此,当您在设计器中调整控件的大小时,消息框的代码就会触发。

    【讨论】:

    • +1。检查if (!DesignMode) 应该有助于解决 OP 的问题。
    • 在构造函数中检查(!DesignMode)不会有帮助(因为尚未创建对象,设计者无法设置DesignMode = true)。
    • @max:好点;要么有条件地在OnLoad 上注册处理程序,要么有条件地抑制处理程序本身的操作。
    • 这在构造函数中有效:bool designMode = LicenseManager.UsageMode == LicenseUsageMode.Designtime;
    猜你喜欢
    • 1970-01-01
    • 2012-03-13
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-02
    • 1970-01-01
    相关资源
    最近更新 更多