
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 PropertyGrid
s you want to see:

- Include Container Controls - This will make property grids available for any control that is a container control, such as
Form
, Panel
, GroupBox
, etc.
- 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 ;
}
}
}
- Include Buttons and Labels - This will make property grids available for any
Button
or Label
.
- '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 TextBox
es, 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
private void AddItem(object o)
{
if (List.ContainsValue(o) == false)
{
List.Add(List.Count,o) ;
}
}
AddButtonsAndLabels
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
private void AddOtherControls(Control control)
{
if (IsOtherControl(control))
{
AddItem(control) ;
}
if (control.HasChildren)
{
foreach (Control child in control.Controls)
{
AddOtherControls(child) ;
}
}
}
private bool IsOtherControl(Control 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

After Shot

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