Click here to Skip to main content
14,241,167 members

Resolve DesignMode for a User Control

Rate this:
5.00 (10 votes)
Please Sign up or sign in to vote.
5.00 (10 votes)
26 Aug 2012CPOL
The tip shows one way to resolve if a user control is in design mode.

Introduction

Sometimes, you need to know inside a user control if the application is in design mode or not. For example, if the control is getting data from a database or from a web service, the connection may not be established at design time.

A control contains a property called DesignMode, but examining just its value isn't enough. The DesignMode should be evaluated for the control and for the whole parent hierarchy in order to know the status of the application.

Another thing is that the DesignMode is a protected property, so it cannot be examined from outside the control in a 'traditional' way.

So the motivation for this tip was to create a simple extension method which recursively examines the DesignMode and can be used from outside the control.

The Extension Method

The extension method is quite simple:

namespace Controls {
   public static class Extensions {
      /// <summary>
      /// Extension method to return if the control is in design mode
      /// </summary>
      /// <param name="control">Control to examine</param>
      /// <returns>True if in design mode, otherwise false</returns>
      public static bool IsInDesignMode(this System.Windows.Forms.Control control) {
         return ResolveDesignMode(control);
      }
 
      /// <summary>
      /// Method to test if the control or it's parent is in design mode
      /// </summary>
      /// <param name="control">Control to examine</param>
      /// <returns>True if in design mode, otherwise false</returns>
      private static bool ResolveDesignMode(System.Windows.Forms.Control control) {
         System.Reflection.PropertyInfo designModeProperty;
         bool designMode;
 
         // Get the protected property
         designModeProperty = control.GetType().GetProperty(
                                 "DesignMode", 
                                 System.Reflection.BindingFlags.Instance 
                                 | System.Reflection.BindingFlags.NonPublic);
 
         // Get the controls DesignMode value
         designMode = (bool)designModeProperty.GetValue(control, null);
 
         // Test the parent if it exists
         if (control.Parent != null) {
            designMode |= ResolveDesignMode(control.Parent);
         }
 
         return designMode;
      }
   }
}

So the extension method simply calls the private ResolveDesignMode method. This method travels recursively through all the parents of the control to resolve if any control is in design mode.

The method finds out the DesignMode property value for the control at hand. Since the property is protected by design, getting the value by simply querying the property isn't possible. This is why reflection is used to get the property and then the value of the property. In order to get the property, NonPublic binding flag must be used.

The Test Control

The test control contains just a textbox. When the control is created, the text of the textbox is set to describe if the control or any of its parents is in design mode or not.

The code is simple:

namespace Controls {
   public partial class TestControl : UserControl {
      public TestControl() {
         InitializeComponent();
      }
 
      private void TestControl_Load(object sender, EventArgs e) {
         // Is this control or one of its parents in design mode
         this.txtText.Text = this.IsInDesignMode() 
                             ? "Program is in design mode" 
                             : "The program is running";
      }
   }
}

The code to inspect if the design mode is true is written to the Load event. This is because when the control is created on a window, its parent is set later in the designer generated code. This is why the parents cannot be found inside the constructor.

The Test Project

The test project contains a single window and two instances of the test control. One is placed on the window and the other one inside few containers in order to test that the parents are resolved correctly.

When in design mode, the window looks like:

And at run-time:

Final Words

Hopefully, this extension method helps to resolve the status of an application from inside or if needed, also from outside a user control.

Note: Before opening the window in the test project, remember to compile the whole solution since the download doesn't contain any binaries.

History

  • 26th August, 2012: Created

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Wendelius
Architect
Europe Europe
This member does have enough reputation to be able to display their biography and homepage.







Comments and Discussions

 
GeneralMy vote of 5 Pin
Francesco Giossi23-Jul-14 0:26
memberFrancesco Giossi23-Jul-14 0:26 
GeneralMy vote of 5 Pin
TeoMan14-Dec-12 4:17
memberTeoMan14-Dec-12 4:17 
GeneralRe: My vote of 5 Pin
Wendelius14-Dec-12 8:35
mveWendelius14-Dec-12 8:35 
GeneralMy vote of 5 Pin
Espen Harlinn26-Aug-12 22:30
mentorEspen Harlinn26-Aug-12 22:30 
GeneralRe: My vote of 5 Pin
Wendelius31-Aug-12 8:28
mveWendelius31-Aug-12 8:28 
GeneralMy vote of 3 Pin
nobodyxxxxx26-Aug-12 20:22
membernobodyxxxxx26-Aug-12 20:22 
GeneralRe: My vote of 3 Pin
Wendelius31-Aug-12 8:27
mveWendelius31-Aug-12 8:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Tip/Trick
Posted 26 Aug 2012

Stats

36.7K views
594 downloads
8 bookmarked