ARTICLE

Object Inspector in C#

Posted by zeppaman Articles | Windows Forms C# April 11, 2006
A cool C# control that allows to control and manage all the controls in a form at runtime.
Reader Level:
Download Files:
 

objectinspector.jpg 

What's new?

In this update I added some cool features like:

  • Recompiled for framework2 
  • Revisited the graphic interface 
  • Added a info panel on the footer 
  • More properties are exposed 
  • Some refactoring methods are exposed 
  • Added the show method list 
  • Added the show type attribute 
  • Added the preview Control in new Form

Introduction

The framework give us a useful component to show information's about a control. I'm speaking about the property grid. It's a beautiful control but unfortunately it don't list controls, so the user can select a specific control. I try to resolve this problem with this control.

Object Inspector

Object inspector is an advanced property grid that list and show every control on the form. When user selects one item, Object Inspector shows all its properties. An alternative to list all the properties is to click the "Show properties button" that display like this:

propertylist.jpg

Finding all components on loading (or when user call reload) the control scans the form to find every control. The search is obviously recursive and my solutions(maybe not the best...) is shown below:

public void FindChild(Control par)

{

          foreach (Control cc in par.Controls)

          {

                   if (cc.Name!=null&&cc.Name!="")

                             this.comboBox1.Items.Add(cc.Name);

                   if(cc.Controls.Count>0)

FindChild(cc);

          }

}

 

public void FindEach()

{

          this.comboBox1.Items.Clear();

          if (this.Parent!=null&&this.Parent.Controls.Count>0)

                   foreach (Control cc in this.Parent.Controls)

                   {

                             this.comboBox1.Items.Add(cc.Name);

                             if (cc.Controls.Count > 0)

FindChild(cc);

                             }

}

Finding the selected component

When user selects a control on the list, this control searches on the form's control the right object. Also in this case I used a recursive search. When the right object has been found, I send it to the property grid to show all its features.

public Control FindChildByName(ref string name,Control par)

{

          Control c=null;

          foreach (Control cc in par.Controls)

          {

                   if(cc.Name==name)

                             return cc;

                    if(cc.Controls.Count>0)

                   {

                             c= FindChildByName(ref name,cc);

                             if (c!=null)

                                       return c;

                   }

          }

          return null;

}

 

public Control FindObjectByName(string name)

{

          Control c=null ;

          if (this.Parent!=null&&this.Parent.Controls.Count>0)

                   foreach (Control cc in this.Parent.Controls)

                   {

                             if(cc.Name==name)

                                      return cc;

                             if (cc.Controls.Count > 0)

                             {

                                      c= FindChildByName(ref name,cc);

                                      if (c!=null)

                                                return c;

                             }

                   }

return null;

}

Credits

This is only one of the possible solutions. Maybe isn't the better, but I think that's simple and fast. For every advice, question or problem please contact me. If you want to see my other work please visit my home page:http://zeppaman.altervista.org

COMMENT USING