Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Dynamic Runtime Property Viewer

0.00/5 (No votes)
6 Jul 2005 3  
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:

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.
    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

//method to add a control to the SortedList

private void AddItem(object o)
{
    if (List.ContainsValue(o) == false)
    {
        List.Add(List.Count,o) ;    
    }
}

AddButtonsAndLabels

//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

//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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here