Click here to Skip to main content
Click here to Skip to main content

Resolve DesignMode for a user control

, 26 Aug 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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 it's 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 text box. When the control is created the text of the textbox is set to describe if the control or any of it's 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, it's parent is set later in the designer generated code. This is why the parents cannot be found inside the constructor.

The test poject

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

Mika Wendelius
Architect
Finland Finland
I've been a programmer since mid 80's using languages like assembler, C/C++, PL/I (mainframe environment), pascal, VB (I know, I know, no comments please) and C# and utilizing different techniques and tools.
 
However I'm specialized in databases and database modeling. Mostly I have used products like Oracle (from version 6), SQL Server (from version 4.2), DB2 and Solid Server (nowadays an IBM product).
 
For the past 10+ years my main concerns have been dealing with different business processes and how to create software to implement and improve them. At my spare time (what ever that actually means) I'm also teaching and consulting on different areas of database management, development and database oriented software design.

Comments and Discussions

 
GeneralMy vote of 5 PinmemberFrancesco Giossi23-Jul-14 0:26 
GeneralMy vote of 5 PinmemberTeoMan14-Dec-12 4:17 
GeneralRe: My vote of 5 PinmvpMika Wendelius14-Dec-12 8:35 
GeneralMy vote of 5 PinmvpEspen Harlinn26-Aug-12 22:30 
GeneralRe: My vote of 5 PinmvpMika Wendelius31-Aug-12 8:28 
GeneralMy vote of 3 PinmemberH. Mueller26-Aug-12 20:22 
GeneralRe: My vote of 3 PinmvpMika Wendelius31-Aug-12 8:27 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 26 Aug 2012
Article Copyright 2012 by Mika Wendelius
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid