Live Chat Icon For mobile
Live Chat Icon

How do I add a custom tab to the property grid when it is displaying my object’s properties?

Platform: WinForms| Category: UI

We have a small sample that shows how to do this. Download PropTab.zip. After you download and unzip the sample, build the project. Both the control assembly and a small driver assembly get built. After that add the control to your toolbox using ’Customise toolbox…’. Then drag and drop an instance of the added control onto the form in the driver winforms sub-project. When you select this control the properties window should have a tab with a bitmap ’T’ as shown below.

These are the steps involved. Before following these steps please download and take a look at the sample. That will greatly help when following these steps.

Control related steps

Assume that you have a control that has two sets of properties, one set that you wish to have displayed in the main property tab and another set that you wish to have displayed in the second tab.

  • Mark those properties that you wish to display in the first tab with the BrowsableAttribute to true.
  • Mark those properties that you wish to display in the second tab with the BrowsableAttribute set to false. Create another attribute. Name it anything you want and give it a single boolean property. Initialize this property to true.

Other steps

  • Derive a class from System.Windows.Forms.PropertyGridInternal.PropertiesTab. You have to override a few methods. The most important of these is GetProperties. We override GetProperties as shown below to use our CustomAttribute as the filtering attribute instead of the BrowsableAttribute that the PropertyGrid uses by default.
    
    public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object component, Attribute[] attrs)
        {
          return TypeDescriptor.GetProperties(component, new Attribute[]
            {new BrowsableAttribute(false), new CustomTabDisplayAttribute(true)});
        }
    
  • Create a embedded resource bitmap with the same name as the derived tab’s type. This bitmap had to be 16×16.

A brief note of explanation on the sample. The sample shows a user control that displays its own tab for some properties. These properties are marked with the filtering attribute, CustomTabDisplayAttribute. The rest of the properties are just displayed in the normal property tab and need no special attention.

To get a deeper understanding of how this works, please refer to these FAQs on TypeConverters and TypeDescriptors.

Share with