【问题标题】:What event should I handle to execute code when WinForms application switches from Run mode to Design mode当 WinForms 应用程序从运行模式切换到设计模式时,我应该处理什么事件来执行代码
【发布时间】:2010-04-20 19:06:47
【问题描述】:

我正在运行一个 Windows 窗体应用程序,当我切换到设计模式时,我需要执行一段代码。我有一个 OnEnterDesignMode 调试器事件的处理程序,如果我正在调试应用程序然后切换到设计模式,这会受到影响。但是,如果我最初没有调试就开始,然后切换到设计模式,这不会受到影响。从运行模式切换到设计模式时,我需要处理什么事件才能执行某些代码?

【问题讨论】:

  • 应用程序如何从运行模式切换到设计模式?

标签: c# .net winforms


【解决方案1】:

你指的是发布模式和调试模式吗?

如果是这样,您可以将代码包装如下:

#if DEBUG
            //Execute debug mode code
#else
            //execute release mode code
#endif

【讨论】:

    【解决方案2】:

    不确定该事件,但 Windows 窗体上有一个 DesignMode bool 属性,可能继承自 Control,如果窗体在设计器中打开,则返回 true。但请注意,即使在设计模式下,DesignMode 在构造函数中也会返回 false。所以总是在诸如加载事件之类的东西中使用它,而不是在构造函数中。

    【讨论】:

      【解决方案3】:

      在您的控件中尝试 Component.DesignMode。见MSDN

      VB.net 示例

       Private Sub txtSmartDate_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSmartDate.TextChanged
          If Not Me.DesignMode Then
              If _ValueInitialised And Not _SuppressEventCode Then
                  '   Apply the changes to the property value, now the text box has been updated.
                  Call SetPropertyValues()
                  Call ApplyDateFormating()
              End If
          End If
      End Sub
      

      【讨论】:

        猜你喜欢
        • 2022-12-19
        • 2017-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多