Click here to Skip to main content
15,881,172 members
Articles / Programming Languages / C#
Article

Dynamic Runtime Property Viewer

Rate me:
Please Sign up or sign in to vote.
4.61/5 (28 votes)
6 Jul 2005CPOL3 min read 80.2K   1.7K   67   10
Use the property grid to adjust any control in your application during runtime.

Sample Image - RunTimePropertyViewer.jpg

Introduction

I have spent just too much time running my applications just so I can see how different color schemes, or other control settings look at runtime. How many times have you restarted your app just so you could see how a control looked with a different background color, or how a grid looks with alternating row colors?

Well, I decided to create this little control that will allow you to specify which types of controls you want to be able to adjust properties on at run time. Keep in mind that this is definitely not a component that you want users to have access to! So, make sure that only developers can open this thing up.

Usage Summary

The 'RunTimePropertyViewer' project is a control library project. Just reference the DLL in your project, and from any screen you will be able to edit the properties of any type of control that lives anywhere on your screen.

The syntax is simple:

C#
RunTimePropertyViewer.PropertyViewer pv = 
         new RunTimePropertyViewer.PropertyViewer(this) ;
pv.Show() ;

How it Works

When you instantiate the PropertyViewer object, you pass in a reference to a ContainerControl. This can be a Form, or any kind of user control that is a ContainerControl.

The constructor of the PropertyViewer will hang on to the ContainerControl reference, but won't do anything until you click the GO button.

There are four choices you have for telling the PropertyViewer what PropertyGrids you want to see:

Choices

  1. Include Container Controls - This will make property grids available for any control that is a container control, such as Form, Panel, GroupBox, etc.
  2. Include Standard .NET Data Controls - This will make property grids available for standard data controls. You can change what controls are seen as 'standard' by editing the AddStandardDataControl method.
    C#
    private void AddStandardDataControls(Control control)
    {
        if (control.HasChildren)
        {
            foreach (Control child in control.Controls)
            {
                AddStandardDataControls(child) ;
            }
        }
        else
        {
            if (control is TextBoxBase || control is CheckBox || 
            control is DateTimePicker || control is NumericUpDown || 
            control is DataGrid || control is ListBox)
            {
                AddItem(control) ;
                return ;
            }
        }
    }
  3. Include Buttons and Labels - This will make property grids available for any Button or Label.
  4. 'Other Types' - This could be a bit misleading. Basically, you can enter a comma-separated list of any control type, and the property grid will be made available for those types. If for example, you have a custom user control called 'MyControl1', and you want to see property grids for all the 'MyControl1' controls, as well as TextBoxes, you would enter "MyControl1,TextBox" in the 'Other Types' field, and uncheck the other three 'include' checkboxes. This is not case sensitive, but if you misspell a control type, it will be ignored.

Once you hit the GO button, and you get a list of available controls, just navigate the DataGrid to control whose property grid you wish to see. The currencyManager behind the form will 'hook' to the right property grid.

OK, the Code

There are quite a few similar methods in this control, but I'll point out a few of them:

AddItem

C#
//method to add a control to the SortedList
private void AddItem(object o)
{
    if (List.ContainsValue(o) == false)
    {
        List.Add(List.Count,o) ;    
    }
}

AddButtonsAndLabels

C#
//add button and label controls to the list, if this 
//  option is specified
//This is a recursive function, so it will run for every 
//  control on every container control
private void AddButtonsAndLabels(Control control)
{
    if (control is Button || control is Label)
    {
        AddItem(control) ;
    }
    else if (control.HasChildren)
    {
        foreach (Control child in control.Controls)
        {
            AddButtonsAndLabels(child) ;
        }
    }
}

AddOtherControls and IsOtherControl

C#
//add other control, as specified in the textbox.  
//NOTE:  if you just wanted to see one type of control, you could enter it here.
//  for example, just enter 'textbox'
private void AddOtherControls(Control control)
{
    if (IsOtherControl(control))
    {
        AddItem(control) ;
    }
    if (control.HasChildren)
    {
        foreach (Control child in control.Controls)
        {
            //your 'other' control might have more 'other' controls on it
            AddOtherControls(child) ;
        }
    }
}

//Returns a true if the control type matches what you entered in
//the 'Other' types field.
private bool IsOtherControl(Control control)
{
    //get the lowercase type of the control
    string ctlType = control.GetType().ToString() ;
    ctlType = ctlType.Substring(ctlType.LastIndexOf(".") + 1).ToLower() ;
    
    for (int i = otherControls.GetLowerBound(0) ; i 
           <= otherControls.GetUpperBound(0) ; i++ )
    {
        if (ctlType.CompareTo(otherControls[i])==0)
        {
            return true ;
        }
    }
        return false ;  
}

Conclusion

I have included a demo project, which is an EXE, and I've also included the 'source', which has the test project in it.

Below are 'before' and 'after' images of the TestForm. The properties were changed at runtime through the PropertyViewer.

Before Shot

Sample screenshot

After Shot

Sample screenshot

...And a request

If you like this, please vote.

History

  • 06-Jul-2005 - Fix to display property grid for first item in list when Go button is pressed.

License

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


Written By
Choice Genetics US
United States United States
Seasoned IT Professional. Currently the IT Director for Choice Genetics, and also the world-wide manager for IT Projects related to R&D for Groupe Grimaud (our parent company).

I've spent about half my career as a contractor. I've lived all over the place, but currently reside near St. Louis, Missouri. Moved out here from Roseville, California in Feb-2005. No regrets yet.

Over the recent years I've written software for:
- Disposing of radioactive and toxic waste.
- Disposing of surplus inventory during the Decommission of McClellan Air Force Base.
- Facilitating genetic improvement for Swine Breeding.
- Managing children placed in State custody.
- Dealing with commercial trucking delivery schedules.
- Tracking high resolution images from archeological digs.
- Project Management for the roofing industry.
- Processing engines for credit card transactions.

Comments and Discussions

 
Generaldynamic property viewer Pin
Paresh Gheewala23-Sep-08 23:03
Paresh Gheewala23-Sep-08 23:03 
Hi Paul,

It is indeed a great article.

however I have a User Control which has a custom class called Person
when I try to see the properties it shows only,

(note that Person is my own class which derives from Animal and has Education Smile | :)

RunTimePropertyViewer.Person

I want to browse dynamically all properties of Person and Inner objects ?
how easy it is to do this ?

any other 2 cents, pointers , links would be of great help.

thanks
Paresh.
GeneralProperty grid never populated in Demo Pin
SBendBuckeye5-Jul-05 2:32
SBendBuckeye5-Jul-05 2:32 
GeneralRe: Property grid never populated in Demo Pin
SBendBuckeye5-Jul-05 2:39
SBendBuckeye5-Jul-05 2:39 
GeneralRe: Property grid never populated in Demo Pin
Paul Brower5-Jul-05 13:19
Paul Brower5-Jul-05 13:19 
GeneralRe: Property grid never populated in Demo Pin
Paul Brower6-Jul-05 1:27
Paul Brower6-Jul-05 1:27 
GeneralRe: Property grid never populated in Demo Pin
SBendBuckeye6-Jul-05 2:35
SBendBuckeye6-Jul-05 2:35 
GeneralEnhancement suggestions Pin
fwsouthern30-Jun-05 19:47
fwsouthern30-Jun-05 19:47 
GeneralRe: Enhancement suggestions Pin
Paul Brower1-Jul-05 1:18
Paul Brower1-Jul-05 1:18 
GeneralAnother approach Pin
Corneliu Tusnea30-Jun-05 12:57
Corneliu Tusnea30-Jun-05 12:57 
GeneralRe: Another approach Pin
Paul Brower30-Jun-05 13:30
Paul Brower30-Jun-05 13:30 

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.