开发者

Errors in VisualStudio when opening UserControl in designer

开发者 https://www.devze.com 2022-12-08 18:07 出处:网络
Background: I have created this UserControl. In the constructor of the user control I call a function that retrieves some values from the database. If an error occurs while retrieving the values a mes

Background: I have created this UserControl. In the constructor of the user control I call a function that retrieves some values from the database. If an error occurs while retrieving the values a messagebox explaining the error is shown. So far so good.

The Problem: I have created a form that (among other elements) includes my UserControl. Now when I open this form (or even the UserControl itself) the constructor is called (I suppose so it can be drawn accurately) and, because the database is unavailable, the messagebox (explained above) is shown.

How do I prevent this from happening?

I just want to be clear: the code works great in runtime. Everything is as designed. It is only in Designer view of Visual Studio (2008 SP1 if it matters) where the problem occurs. However in Design开发者_StackOverflower it is awful, especially now when the application attempts to reconnect when the connection fails. Every time I go in to Designer mode my Visual Studio freezes for about 20 seconds (timeout of the reconnect) and it is killing my work process.


You could check whether your control is displayed in design mode:

http://msdn.microsoft.com/en-us/library/system.componentmodel.component.designmode.aspx

/Edit: As people pointed out in the comments to another answer, the DesignMode property is not usable in the constructor. So the best solution would probably be to move the database stuff into an event like "Load" and use the DesignMode property there.


I get around this by having a global static property on my Program class called IsRunning.

When my program starts up in the main method I set the IsRunning property to true. Then in the constructor of my user control I am able to poll the property IsRunning to determine whether or not I execute particular code, in your case code that would attempt to access a database...

EDIT: Here is some code...

private static bool _running = false;

    /// <summary>
    /// Gets or sets a value indicating whether this <see cref="Program"/> is running.
    /// This property is used for design time WSOD issues and is used instead of the 
    /// DesignMode property of a control as the DesignMode property has been said to be
    /// unreliable.
    /// </summary>
    /// <value><c>true</c> if running; otherwise, <c>false</c>.</value>
    public static bool Running
    {
        get
        {
            return _running;
        }
    }


    static void Main(string[] args)
    {
        Initialize();


        _running = true;

....

In my user control...

    public AssignmentList()
    {
        InitializeComponent();

        if (!Program.Running)
        {
            return;
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消