none
Visual Studio tries running application code in DESIGNER VIEW!

    Question

  • I have a Form that is used as the base class for other forms.  The Forms that will inherit this as their base class and add their own buttons and "decorations".  In the base class Load event handler, it makes a MS-Access database call to get items to populate a ListBox.  Very straight-forward.  In the form's Designer view, the ListBox is empty, of course, because the program hasn't run yet.  I can do all the normal Designer view things to the form, like resize it, etc. 

    Now, here is where it gets really weird...

    I created a child Form that inherits this base Form.  (Used the Visual Studio wizard to do this.  Nothing done by hand.)  In DESIGNER VIEW, the child class Form show the ListBox populated with the data from the database!! It apparently ran the Load event from the base class, EVEN THOUGH THE APPLICATION ISN'T RUNNING!!  I'm still in the form Designer!

    Just to make sure I wasn't dreaming, I deleted the database.   Sure enough, when I tried to open the child Form in Design View, Visual Studio throws an error, saying it "Could not find file  ...<the database file>".  Also, when the database is present and I open the child form, I see the normal Access .laccdb file created, which happens when something opens the database.  So I'm 100% sure the child form is hitting the database IN DESIGNER VIEW.  Let me repeat:  the main application is NOT running.

    Note that the base class does NOT do this.  Only the child classes.

    OK, this has got to be a bug.  NO code should be running in the Designer view. This really worries me.  What if a base class controls a motor or initializes some external equipment?  That could be really dangerous.  How do I stop this from happening?



    Saturday, February 20, 2016 3:19 PM

Answers

  • Hi Focus Research,

    >>Is there a setting in Visual studio to turn this off?

    As far as I know, there is no related setting to disable this.  And I think it should be a default design for Visual Studio.

    I suggest you could use the workaround shared by Viorel.

    Best Regards,

    Lake Xiao

    Monday, February 22, 2016 5:55 AM
    Moderator

All replies

  • In order to detect the design mode and adjust the features:

    if( DesignMode)
    {
        // in design mode
        . . .
    }
    else
    {
        // not in design mode
        . . .
    }

    Saturday, February 20, 2016 4:07 PM
  • That worked, thanks.   I'm astonished.  That is new since the last time I worked in VS.

    But I have to say, what the F*** were they thinking?  This should not be the default case!  It is WAY too dangerous to have code run without being explicitly being told to run.

    Is there a setting in Visual studio to turn this off?

    Saturday, February 20, 2016 4:41 PM
  • Hi Focus Research,

    >>Is there a setting in Visual studio to turn this off?

    As far as I know, there is no related setting to disable this.  And I think it should be a default design for Visual Studio.

    I suggest you could use the workaround shared by Viorel.

    Best Regards,

    Lake Xiao

    Monday, February 22, 2016 5:55 AM
    Moderator