What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleMake a control with an object property and display it with a special property editor in VB .NET
DescriptionThis example shows how to make a control with an object property and display it with a special property editor in VB .NET. See the Web page for a description.
KeywordsVB .NET, property, control, property editor, custom property editor
CategoriesControls, VB.NET
The Employee class contains FirstName and LastName properties. It must be serializable so a form can save and restore Employee objects as control properties.
<Serializable()> _
Public Class Employee
    Public FirstName As String
    Public LastName As String

    ' Needed by the deserializer.
    Public Sub New()
    End Sub

    Public Sub New(ByVal first_name As String, ByVal _
        last_name As String)
        FirstName = first_name
        LastName = last_name
    End Sub

    Public Overrides Function ToString() As String
        Return FirstName & " " & LastName
    End Function
End Class
The EmployeeControl class provides a property named TheEmployee of type Employee. The importantm part of this code is the Editor attribute on the TheEmployee property. This attribute tells Visual Studio to use the EmployeeEditor class derived from UITypeEditor to edit the property.
Imports System.Drawing.Design
Imports System.ComponentModel

<ToolboxBitmap(GetType(EmployeeControl), _
    "employee_control.bmp"), _
 DefaultProperty("TheEmployee")> _
Public Class EmployeeControl
    Inherits System.Windows.Forms.UserControl

... Windows Form Designer generated code ...

    Private m_Employee As Employee

    <Editor(GetType(EmployeeEditor), _
        GetType(UITypeEditor)), _
     Category("Data"), _
     DefaultValue(GetType(Employee), Nothing)> _
    Public Property TheEmployee() As Employee
            Return m_Employee
        End Get
        Set(ByVal Value As Employee)
            m_Employee = Value
        End Set
    End Property

    Private Sub UserControl1_Paint(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.PaintEventArgs) _
        Handles MyBase.Paint
        If Not (m_Employee Is Nothing) Then
            e.Graphics.DrawString(m_Employee.ToString(), _
                Me.Font, Brushes.Purple, 0, 0)
        End If
    End Sub
End Class
The EmployeeEditor class inherits from UITypeEditor. Its GetEditStyle function returns UITypeEditorEditStyle.Modal to indicate that it uses a modal form when editing properties.

The EditValue method creates a IWindowsFormsEditorService object, initializes a frmEmployeeEditor form, and calls the editor service's ShowDialog method to display the dialog. If the user clicks OK, it copies the results into its value parameter.

Imports System.Drawing.Design
Imports System.Windows.Forms.Design

Friend Class EmployeeEditor
    Inherits UITypeEditor

    ' We display a modal dialog.
    Public Overloads Overrides Function GetEditStyle(ByVal _
        context As _
        System.ComponentModel.ITypeDescriptorContext) As _
        Return UITypeEditorEditStyle.Modal
    End Function

    ' Edit the value.
    Public Overloads Overrides Function EditValue(ByVal _
        context As _
        System.ComponentModel.ITypeDescriptorContext, ByVal _
        provider As System.IServiceProvider, ByVal value As _
        Object) As Object
        ' Get an IWindowsFormsEditorService.
        Dim editor_service As IWindowsFormsEditorService = _
            CType( _
                provider.GetService( _
                    GetType(IWindowsFormsEditorService)), _
        If editor_service Is Nothing Then Return Nothing

        ' Display the input dialog.
        Dim first_name As String
        Dim last_name As String
        If value Is Nothing Then
            first_name = ""
            last_name = ""
            Dim the_setting As Employee = CType(value, _
            first_name = the_setting.FirstName
            last_name = the_setting.LastName
        End If
        Dim dlg As New frmEmployeeEditor
        dlg.txtFirstName.Text = first_name
        dlg.txtLastName.Text = last_name

        ' Display the editor form.
        If editor_service.ShowDialog(dlg) = DialogResult.OK _
            ' The user clicked OK. Use the new value.
            first_name = dlg.txtFirstName.Text.Trim
            last_name = dlg.txtLastName.Text.Trim
            If (first_name.Length = 0) And _
                (last_name.Length = 0) Then
                value = Nothing
                value = New Employee(first_name, last_name)
            End If
        End If

        ' Return the value.
        Return value
    End Function
End Class
See the code for additional details.

Note that custom editors don't seem to work with properties that are enumerated types. Visual Studio seems to think that the enumerated type dropdown is sufficient for those values.

Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.