Consider a WinForms application with a form Form1 and a UserControl UserControlA.
If you drag and drop UserControlA on to Form1 in the VS2005 Designer, then the Designer will execute UserControlA's Load event handler(s) each time Form1 is opened in the Designer.
This can be highly undesirable, for example a Load event handler may contain application code to connect to a database etc that should not be executed at design time.
In the above scenario, a workaround is to test the DesignMode property in the Load event handler, and skip unwanted processing if DesignMode is true.
However, this workaround does not work for nested UserControls.
Consider a project with a form Form1, and two UserControls UserControlA and UserControlB. Drag and drop UserControlB on to UserControlA, and UserControlA on to Form1. Build then open Form1 in the VS designer.
In this case, UserControlB's Load event is executed by the designer, and the DesignMode property is set to false.
For example, if UserControlB's Load event handler contains the following code, the MessageBox below is displayed:
| |
private void UserControlB_Load(object sender, EventArgs e) { if (!this.DesignMode) { MessageBox.Show("In UserControlB Load event\n" + Environment.StackTrace); } }
|
Is there any way to prevent code in the Load event handler from being executed by the designer in this general case where UserControls are nested?
Thanks,
Joe
MessageBox displayed by the above code when Form1 is opened in the VS designer:
---------------------------
---------------------------
In UserControlB Load event
at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
at System.Environment.get_StackTrace()
at TestUserControlDesigner.UserControlB.UserControlB_Load(Object sender, EventArgs e) in C:\TestUserControlDesigner\TestUserControlDesigner\UserControlB.cs:line 22
at System.Windows.Forms.UserControl.OnLoad(EventArgs e)
at System.Windows.Forms.UserControl.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.ControlCollection.Add(Control value)
at System.Windows.Forms.Form.ControlCollection.Add(Control value)
at System.Windows.Forms.Design.ControlDesigner.DesignerControlCollection.Add(Control c)
at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeExpression(IDesignerSerializationManager manager, String name, CodeExpression expression)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeStatement(IDesignerSerializationManager manager, CodeStatement statement)
at System.ComponentModel.Design.Serialization.CodeDomSerializer.DeserializeStatementToInstance(IDesignerSerializationManager manager, CodeStatement statement)
at System.ComponentModel.Design.Serialization.CodeDomSerializer.Deserialize(IDesignerSerializationManager manager, Object codeObject)
at System.Windows.Forms.Design.ControlCodeDomSerializer.Deserialize(IDesignerSerializationManager manager, Object codeObject)
at System.ComponentModel.Design.Serialization.TypeCodeDomSerializer.DeserializeName(IDesignerSerializationManager manager, String name, CodeStatementCollection statements)
at System.ComponentModel.Design.Serialization.TypeCodeDomSerializer.Deserialize(IDesignerSerializationManager manager, CodeTypeDeclaration declaration)
at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager manager)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.DeferredLoadHandler.Microsoft.VisualStudio.TextManager.Interop.IVsTextBufferDataEvents.OnLoadCompleted(Int32 fReload)
---------------------------
OK
---------------------------