你可以直接使用一个常量判断啊:比如const isTest=true
通过测试isTest的值来决定是设计还是运行态。在完成设计时,可以把isTest=False

解决方案 »

  1.   

    通过手动改变#const是可以在运行与设计时改变行为,但这样很不方便。更重要的是引用控件的窗体在设计时是经常会被打开或关闭的,这样对控件来说,主程序的设计时也是控件的运行时,所以是没有意义的。多谢您 的关注!
      

  2.   

    HOWTO: Tell If UserControl is in Design-Time or Run-Time --------------------------------------------------------------------------------
    The information in this article applies to:Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, version 6.0 
    Microsoft Visual Basic Control Creation, Professional, and Enterprise Editions for Windows, version 5.0--------------------------------------------------------------------------------
    SUMMARY
    When creating a UserControl in Visual Basic you may have code that you don't what to run when the control is on a Form at design-time, but you do want to run when the Form is in run-time. This article shows how to use the AmbientProperties Object to tell when a host container of a control is in design or run-time. MORE INFORMATION
    When a UserControl is placed on a form, any constituent controls that are in the control are in run-time mode, not design-time mode. Usually this does not cause any trouble for the developer. But there are cases where you need to have an event happen only when the container for the UserControl is in run-time. The following example shows how to enable or disable a Timer control by testing to see if the container is in design-time or run-time. 
    Step-by-Step Example
    Create a New Standard EXE project. 
    From the Project Menu, select Add UserControl. 
    Add a Timer and a Label control to the UserControl. 
    Add the Following Code to the UserControl:       Private Sub Timer1_Timer()
             Label1.Caption = Time()
          End Sub      Private Sub UserControl_Show()
             If Ambient.UserMode Then 'We are in run-time
                Timer1.Interval = 1000
                Timer1.Enabled = True
             Else 'We are in design-time
                Timer1.Enabled = False
                Label1.Caption = "design time"
             End If
          End Sub
     Close the UserControl. 
    Add the UserControl to Form1. 
    Run the Form. 
    Close the Form. 
      

  3.   

    HOWTO: Tell If UserControl is in Design-Time or Run-Time --------------------------------------------------------------------------------
    The information in this article applies to:Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, version 6.0 
    Microsoft Visual Basic Control Creation, Professional, and Enterprise Editions for Windows, version 5.0--------------------------------------------------------------------------------
    SUMMARY
    When creating a UserControl in Visual Basic you may have code that you don't what to run when the control is on a Form at design-time, but you do want to run when the Form is in run-time. This article shows how to use the AmbientProperties Object to tell when a host container of a control is in design or run-time. MORE INFORMATION
    When a UserControl is placed on a form, any constituent controls that are in the control are in run-time mode, not design-time mode. Usually this does not cause any trouble for the developer. But there are cases where you need to have an event happen only when the container for the UserControl is in run-time. The following example shows how to enable or disable a Timer control by testing to see if the container is in design-time or run-time. 
    Step-by-Step Example
    Create a New Standard EXE project. 
    From the Project Menu, select Add UserControl. 
    Add a Timer and a Label control to the UserControl. 
    Add the Following Code to the UserControl:       Private Sub Timer1_Timer()
             Label1.Caption = Time()
          End Sub      Private Sub UserControl_Show()
             If Ambient.UserMode Then 'We are in run-time
                Timer1.Interval = 1000
                Timer1.Enabled = True
             Else 'We are in design-time
                Timer1.Enabled = False
                Label1.Caption = "design time"
             End If
          End Sub
     Close the UserControl. 
    Add the UserControl to Form1. 
    Run the Form. 
    Close the Form.