Replacing Property Pages

Replacing Property Pages

Suppose you re writing an image list user control, and you want to give users a way to add a collection of images. How do you do it? In Visual Basic 6, you would add a property page to your control. In the property page, you would write code that manages the collection. Property pages are useful because they allow you to work around the limitations of the Visual Basic 6 Property Browser, which can edit only strings, enums, numbers, colors, pictures, and fonts. Editing a collection of bitmaps is out of the question for the poor old Visual Basic 6 Property Browser. You ll be pleased to know that Visual Basic .NET has a Property Browser that can edit any .NET variable type or class property pages are no longer needed.

Property pages in user controls are not upgraded automatically. To enable your control to edit the values that used to be exposed in the control s property pages, you need to add properties to your user control. The good news is that the Visual Basic .NET Property Browser automatically recognizes the types of your control s properties and allows users to edit them in the Property Browser. In the case of a bitmap collection, you simply add the property to your user control, and the Visual Basic .NET Property Browser provides a bitmap collection editor. Let s try this out. Add the following code to a Windows Forms user control:

Dim m_bitmapCollection() As Bitmap Property BitmapCollection() As Bitmap()    Get       Return m_bitmapCollection    End Get    Set(ByVal Value As Bitmap())       m_bitmapCollection = Value.Clone    End Set End Property

If you add the control to a form, you will notice that it has a new property, bitmapCollection, and that you can edit this property using a collection editor. Figure 15-9 shows this editor. It s that s simple.

Figure 15-9

Adding a bitmap collection to your user control.

There are property page editors for every .NET class. For example, if you add a targetDate property of type Date to your user control, the Property Browser automatically provides a date editor. Try putting this property code into your user control:

Private m_targetDate As Date Public Property targetDate() As Date    Get       Return m_targetDate    End Get    Set(ByVal Value As Date)       m_targetDate = Value    End Set End Property

The Property Browser automatically uses the date editor for the property, as shown in Figure 15-10.

Figure 15-10

Date editor provided by the Property Browser.

The advantage of showing properties in the Property Browser instead of in property pages is that in the Property Browser all of the properties for your control are visible and easily discoverable. Compare this with Visual Basic 6 property pages, which hide functionality from people who use your controls.

Writing Your Own Property Editors

The Windows Forms Property Browser can edit any .NET class or variable type. But what happens if you write a new class, say a Customer class, and you want users to be able to edit it in the Property Browser? Windows Forms gives you a way to provide this option by allowing you to write your own custom property editor that will be used whenever people edit properties of type Customer. To learn more about writing custom property editors, search for the following two topics in the Visual Basic .NET Help system: Attributes and Design-Time Support and Implementing a UI Type Editor.



Upgrading Microsoft Visual Basic 6.0to Microsoft Visual Basic  .NET
Upgrading Microsoft Visual Basic 6.0 to Microsoft Visual Basic .NET w/accompanying CD-ROM
ISBN: 073561587X
EAN: 2147483647
Year: 2001
Pages: 179

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net