Export (0) Print
Expand All

How to: Perform Custom Initialization for Controls in Design Mode

You can use your custom designer to initialize components and controls as they are created by the design environment.

The following code example demonstrates how to initialize a control when it is created by the design environment. This creation occurs when you drag an instance of the control onto your form, and it also occurs when you start the designer for your form. For a complete explanation of this code example, see How to: Extend the Appearance and Behavior of Controls in Design Mode.

' This demonstrates changing the appearance of a control while 
' it is being designed. In this case, the BackColor property is 
' set to LightBlue.  
Public Overrides Sub InitializeNewComponent( _
ByVal defaultValues As IDictionary)

    MyBase.InitializeNewComponent(defaultValues)

    Dim colorPropDesc As PropertyDescriptor = _
    TypeDescriptor.GetProperties(Component)("BackColor")

    If colorPropDesc IsNot Nothing AndAlso _
       colorPropDesc.PropertyType Is GetType(Color) AndAlso _
       Not colorPropDesc.IsReadOnly AndAlso _
       colorPropDesc.IsBrowsable Then
        colorPropDesc.SetValue(Component, Color.LightBlue)
    End If 
End Sub

When the design environment creates an instance of your control or component, it calls your designer's InitializeNewComponent method. In the previous code example, the control's BackColor property is set by using a PropertyDescriptor.

When you make changes to the design-time aspects of a component, you need to rebuild the control project. In addition, if there is another Windows Forms project that is currently open and uses this component, you will probably need to refresh the project to see the changes. Typically, you need to close and reopen the design window containing the component.

Note Note

You must add a reference to the design-time assembly, System.Design.dll. This assembly is not included in the .NET Framework 4 Client Profile. To add a reference to System.Design.dll, you must change the project's Target Framework to .NET Framework 4.

Show:
© 2014 Microsoft