WinForms FAQ - UI

Find answers for the most frequently asked questions
Expand All Collapse All

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.

Permalink

Set the Browsable attribute on the new property that hides the existing one. Here is a code snippet that hides the WordWrap property in the derived TextBox.

[C#]
public class MyTextBox : TextBox
{
	[Browsable(false)]
	public new bool WordWrap
	{
		get{ return false;} //always false
		set{}
	}
}

[VB.NET]
Public Class MyTextBox
	Inherits TextBox
    
	  _
	Public Shadows Property WordWrap() As Boolean
		Get
			Return False ’always false
		End Get
		Set ’empty
		End Set
	End Property
End Class ’MyTextBox 
Permalink

Share with

Couldn't find the FAQs you're looking for?

Please submit your question and answer.