Linda,
I have experimented with the ISupportInitialize interface, but without
success. I have copied some very simplified code below to demonstrate how I
have tried to use this interface. Note that I am applying the interface to a
derived control instead of the derived form as the original message stated.
This seemed like the more correct location for my application.
When I try to display the "DerivedForm" with the Forms Designer, the code
that causes the assert is still being reached, even though that code is
called from the EndInit() method and includes a test for DesignMode.
Please look at the code below and let me know if I am missing something.
Thanks,
Dave
===============Code Starts Here=================
public class MyPanelBase : System.Windows.Forms.UserControl,
ISupportInitialize
{
public MyPanelBase()
{
InitializeComponent();
}
protected virtual void FinishConstruction()
{
DoSomethingThatAssertsAtDesignTime();
}
// Virtual method overridden by derived class.
protected virtual void DoSomethingThatAssertsAtDesignTime()
{}
public void BeginInit()
{}
public void EndInit()
{
if ( !this.DesignMode )
{
DoSomethingThatAssertsAtDesignTime();
}
}
}
public class DerivedPanel : MyPanelBase
{
public DerivedPanel() : base()
{
InitializeComponent();
}
protected override void DoSomethingThatAssertsAtDesignTime()
{
DoSomethingAtRuntime(); // Causes VS2003 Assert at DesignTime.
}
private void InitializeComponent()
{
((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
this.SuspendLayout();
// Controls go here...
((System.ComponentModel.ISupportInitialize)(this)).EndInit();
this.ResumeLayout(false);
}
}
public class DerivedForm : ChildFormBase
{
private DerivedPanel myDerivedPanel;
public DerivedForm() : base()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.myDerivedPanel = new DerivedPanel();
((System.ComponentModel.ISupportInitialize)(this.myDerivedPanel)).BeginInit();
this.SuspendLayout();
//
// myDerivedPanel
//
this.myDerivedPanel.Location = new System.Drawing.Point(0, 50);
this.myDerivedPanel.Name = "myDerivedPanel";
this.myDerivedPanel.Size = new System.Drawing.Size(410, 140);
this.myDerivedPanel.TabIndex = 0;
//
// DerivedForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(7, 17);
this.ClientSize = new System.Drawing.Size(432, 643);
this.Controls.Add(this.myDerivedPanel);
this.Icon =
((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "DerivedForm";
this.Text = "My Form";
((System.ComponentModel.ISupportInitialize)(this.myDerivedPanel)).EndInit();
this.ResumeLayout(false);
}
}
===============Code Ends Here==================
--
Dave Leach
Agilent Technologies, Inc.
[quoted text, click to view] "Linda Liu [MSFT]" wrote:
> Hi Dave,
>
> Firstly, when we open a form in the designer, the code in the form's
> constructor won't be called.
>
> You have mentioned that you have a section of code in one of your form's
> constructors that needs to be skipped over if the constructor is being
> called at design time. It seems that the section of code must be in a base
> form's constructor, and what you open in the form designer is an inherited
> form.
>
> It's no use to make use of the form's DesignMode property in the
> constructor to determine whether it is at design time or run time, because
> the Site property of the form has not been assigned when the form is being
> constructed. The DesignMode property of the form always returns false when
> we get its value in the constructor.
>
> When the form's Load event handler is called, the form has already finished
> the construction and the DesignMode property has a valid value. So the
> solution is to move the section of code that you need to prevent from being
> executed at design time to the form's Load event handler, and add a
> condition of "! this.DesignMode" on the section of code.
>
> Alternatively, you could implement the ISupportInitialize interface for the
> base form and move the section of code to either of the ISupportInitialize
> members -- BeginInit or EndInit method (of course, you still need to add "!
> this.DesignMode" condition on the section of code).
>
> When the base form implements the ISupportInitialize interface and we open
> the inherited form in the designer, the code to call the
> ISupportInitialize.BeginInit and ISupportInitilize.EndInit methods are
> serialized in the InitializeComponent method of the inherited form. When
> the ISupportInitialize.BeginInit or ISupportInitialize.EndInit method is
> executed, the base form has already finished the construction, thus the
> DesignMode property returns a valid value at this time.
>
> Hope this helps.
> If you have anything unclear, please feel free to let me know.
>
>
> Sincerely,
> Linda Liu
> Microsoft Online Community Support
>
> ==================================================
> Get notification to my posts through email? Please refer to
>
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif > ications.
>
> Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
> where an initial response from the community or a Microsoft Support
> Engineer within 1 business day is acceptable. Please note that each follow
> up response may take approximately 2 business days as the support
> professional working with you may need further investigation to reach the
> most efficient resolution. The offering is not appropriate for situations
> that require urgent, real-time or phone-based interactions or complex
> project analysis and dump analysis issues. Issues of this nature are best
> handled working with a dedicated Microsoft Support Engineer by contacting
> Microsoft Customer Support Services (CSS) at
>
http://msdn.microsoft.com/subscriptions/support/default.aspx. > ==================================================
>
> This posting is provided "AS IS" with no warranties, and confers no rights.
>