Take full advantage of the. NET Framework's PropertyGrid Control (Microsoft) VIII

Source: Internet
Author: User
Tags domain custom graphics domain list visual studio advantage
Controls | Microsoft Add domain list and simple Drop-down list attribute support
For properties that return enumerations based on the enum type, PropertyGrid automatically displays the enumeration values in the Drop-down list. Enumconverter also provides this functionality. For your own properties, you may want to provide the user with a list of valid values (sometimes referred to as a pick list or domain list), and the type is not based on an Enum. This is the case if the domain value is unknown before the runtime, or if the value can be changed.

Modify the Options window to provide a list of domains from which users can select a default filename. You have added the Defaultfilename property to the AppSettings class. The next step is to display the Drop-down list of properties in PropertyGrid to provide a list of domains.

Provides simple Drop-down list attribute support
Creates a class that inherits from the type converter class. Because the Defaultfilename property belongs to the String type, it can be inherited from Stringconverter. If the type converter for the property type does not exist, you can inherit from TypeConverter;
' Visual Basic

Public Class Filenameconverter
Inherits Stringconverter
End Class

Overwrites the GetStandardValuesSupported method and returns True, indicating that this object supports a set of standard values that can be selected from the list.
' Visual Basic

Public Overloads Overrides Function getstandardvaluessupported (_
ByVal context as ITypeDescriptorContext) as Boolean
Return True
End Function

Overwrites the GetStandardValues method and returns the standardvaluescollection filled with the standard value. One way to create StandardValuesCollection is to provide an array of values in the constructor. For option window applications, you can use a String array populated with the recommended default file name.
' Visual Basic

Public Overloads Overrides Function GetStandardValues (_
ByVal context as ITypeDescriptorContext) _
As StandardValuesCollection

return new StandardValuesCollection (new String () {???, _
"File 1", _
"Document 1"})
End Function

Optionally, if you want users to be able to type a value that is not contained in the Drop-down list, overwrite the GetStandardValuesExclusive method and return False. This essentially turns the Drop-down list style into a combo box style.
' Visual Basic

Public Overloads Overrides Function GetStandardValuesExclusive (_
ByVal context as ITypeDescriptorContext) as Boolean
Return False
End Function

Once you have your own type converter class to display the Drop-down list, you need to determine the target of using that class. In this example, the target is the Defaultfilename property, because the type converter is for the property. Apply the TypeConverterAttribute to the target attribute.
' Visual Basic

' Applies to the TypeConverter attribute of the Defaultfilename property.
<typeconverter (GetType (Filenameconverter)), _
CategoryAttribute ("Document Settings") > _
Public Property Defaultfilename () as String
Get
Return _defaultfilename
End Get
Set (ByVal Value as String)
_defaultfilename = Value
End Set
End Property

Compile and run the Option window application again. The following screenshot shows the current appearance of the Options window. Notice the appearance of the Defaultfilename property.

Figure 7: Display the dropdown field list in PropertyGrid

Provide custom UI for properties
As mentioned above, the. NET framework type uses TypeConverter and UITypeEditor classes (as well as other classes) to provide PropertyGrid editing support. For information about how to use TypeConverter, see the section support for custom types; You can also use the UITypeEditor class to customize PropertyGrid.

You can provide small graphical representations and property values in PropertyGrid, similar to what is provided for the Image and Color classes. To perform this action in a customization, inherit from UITypeEditor, overwrite getpaintvaluesupported, and return True. Then, overwrite the Uitypeeditor.paintvalue method and draw the graph using the Paintvalueeventargs.graphics parameter in its own method. Finally, apply the Editor attribute to a class or property that uses the UITypeEditor class.

The following screenshot shows the appearance of the results.

Figure 8: Custom graphics for displaying properties in PropertyGrid

You can also provide your own Drop-down list control, which is similar to the control that the Control.dock property uses to provide the user with a pick-and-click. To do this, inherit from UITypeEditor, overwrite GetEditStyle, and return a Uitypeeditoreditstyle enumeration value, such as DropDown. Your custom Drop-down list control must inherit from a derived class, such as UserControl, that is either controls or ControlSource. Then, overwrite the Uitypeeditor.editvalue method. Use the IServiceProvider parameter to call the IServiceProvider.GetService method to get a IWindowsFormsEditorService instance. Finally, call the Iwindowsformseditorservice.dropdowncontrol method to display your custom Drop-down list control. Remember to apply the Editor attribute to a class or property that uses the UITypeEditor class.

The following screenshot shows the appearance of the results.

Figure 9: A custom Drop-down list control that displays properties in PropertyGrid

In addition to using the Typeeditor and UITypeEditor classes, you can customize PropertyGrid to display additional property tabs. The Properties tab inherits from the PropertyTab class. If you have used a property browser in Microsoft Visual c#™.net, you may have seen a custom propertytab. The Events tab (a button with a lightning graphic) is a custom propertytab. The following screenshot shows another example of a custom propertytab. You can use PropertyTab to edit the border points of a button to create a custom button shape.

Figure 10: Displaying a custom tab in PropertyGrid

For more information about customizing PropertyGrid with the UITypeEditor class, and for the above custom user interface code example, see Shawn Burke's article make Your components really RAD with Visual Studio. NET Property Browser (English).

Summary
The. NET Framework provides Properygrid controls with rich editing capabilities that you can use to improve your user interface. PropertyGrid customization is very simple, and you can use this control in any application. In addition, because the Visual Studio. NET Property Browser is based on PropertyGrid, you can use these technologies to provide a richer design-time experience.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.