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

Object Inspector

Rate me:
Please Sign up or sign in to vote.
4.27/5 (20 votes)
27 Mar 2006CPOL1 min read 48K   476   33   3
A complete Object Inspector. Some cool features added.
ObjectInspector.jpg

What's New?

In this update, I add some cool features like:

  • Recompiled for framework2
  • Revisited the graphic interface
  • Added an 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 gives us a useful component to show information about a control. I'm speaking about the property grid. It's a beautiful control but unfortunately it does not 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 lists and shows every control on the form. When the 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 shows this is the form:

propertylist.jpg

Finding All Components

On loading (or when user calls reload), the control scans the form to find every control. The search is obviously recursive and my solutions (maybe not the best...) are shown below:

C#
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 controls on the list, this control searches for the right object on the form's control. Also in this case, I use a recursive search. When the right object is found, I send it to the Property grid to show all its features.

C#
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 it is not the best one, but I think that's simple and fast. For any advice, question or problem, please contact me. If you would like to see my other work, please visit my home page at http://zeppaman.altervista.org.

Image 3

License

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


Written By
Chief Technology Officer
Italy Italy
I'm senior developer and architect specialized on portals, intranets, and others business applications. Particularly interested in Agile developing and open source projects, I worked on some of this as project manager and developer.

My programming experience include:

Frameworks \Technlogies: .NET Framework (C# & VB), ASP.NET, Java, php
Client languages:XML, HTML, CSS, JavaScript, angular.js, jQuery
Platforms:Sharepoint,Liferay, Drupal
Databases: MSSQL, ORACLE, MYSQL, Postgres

Comments and Discussions

 
JokeCompatible with framework 1 and 2 Pin
Daniele Fontani28-Mar-06 8:58
professionalDaniele Fontani28-Mar-06 8:58 
GeneralQuite similar to ... Pin
Corneliu Tusnea7-Dec-05 1:15
Corneliu Tusnea7-Dec-05 1:15 
GeneralRe: Quite similar to ... Pin
Daniele Fontani9-Dec-05 3:06
professionalDaniele Fontani9-Dec-05 3:06 

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.