Microsoft KB Archive/175222

= How To Tell If UserControl is in Design-Time or Run-Time =

Article ID: 175222

Article Last Modified on 7/13/2004

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 5.0 Control Creation Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition

-



This article was previously published under Q175222



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.</li> Close the Form.</li></ol>

<div class="references_section">