Search code examples
c#winformsconfiguration-filesdesigner

Winforms - Execute a function when any form is loaded into the designer


I'm working in winforms, and I'm attempting to link in some events any time a form\component\usercontrol is shown in the designer, i'm trying to link in some static events that load preferences from the app.config file.

Is it possible for me to define code in my project that says "any time a control is loaded, execute this event?"

edit this is strictly a design time thing.

I have a base form, in DLL "A". That has a ton of properties, "ColorLocation, SizeLocation", and things of that nature.

In DLL "B" I have a derived form. When B is loaded into the designer, I had iEditorComponents (dont remember the exact name), that allows the user to select ColorLocation from a huge list of items specified in the app.config settings file for the current project.

The problem is, the editing component is in Dll "A", which is the base, and it doesn't have access to the app.config in "B".

I need someway to tell the editing component to "hey, use this list of strings to populate your editing control". The designer is doing everything in its power it seems to not want to execute any code in the derived classes.


Solution

  • Yes, it's possible, but make sure what you're doing, because sounds strange.

    Use the following code in your Form's or Control's constructor:

        public void Form1()
        {
           InitializeComponent();
    
           if (IsInWinFormsDesignMode())
           {
               // your code stuff here
           }
        }
    
        public static bool IsInWinFormsDesignMode()
        {
            bool returnValue = false;
    
            #if DEBUG  
    
            if ( System.ComponentModel.LicenseManager.UsageMode == 
                 System.ComponentModel.LicenseUsageMode.Designtime )
            {
                returnValue = true;
            }
    
            #endif
    
            return returnValue;
        }
    

    Hope it helps.