Previous | Index | Next 

[PRB] The Default and Cancel properties of a VB6UserControl object don’t behave like in VB6

VB6 and VB.NET implement default and cancel buttons in radically different ways: VB6 uses the Default and Cancel properties applied to the button, whereas VB.NET uses the AcceptButton and CancelButton properties exposed by the parent form. VB Migration Partner’s support library fills the gap by providing the Default and Cancel properties in both Command button controls.

VB6 also provides the ability to defined user controls that behave like push buttons. This can be achieved by setting the user control’s design-time DefaultCancel property to True. When migrating such user controls, VB Migration Partner generates a class that exposes the Default and Cancel properties.

This way to mimic VB6’s behavior is nearly perfect, but there are a few cases when manual fixes are required:

a) if a form contains multiple user controls that behave like push buttons, the Default (or Cancel) and if these user controls contain a button whose Default (or Cancel) property is set to True, then the default (or cancel) push button for the form depends on the order in which the user controls are added to the form’s Controls collection. If you notice that the form displays the wrong default button, you should re-assign the Default property of the user control that must work as the default control:

        Private Sub Form_Load() Handles MyBase.Load
            Me.myUserControl1.Default = True
            Me.myUserControl1.Cancel = True
        End Sub

b) if a user control with DefaultCancel=True contains a command button, setting the Default (or Cancel) property of this button to True affects the AcceptButton (or CancelButton) property of the parent form, which ultimately makes the form behave differently from the original VB6 code. Therefore, the code in the user control should avoid setting the Default or Cancel property of a child button if the user control isn’t the default or cancel button on its parent form.

        ' inside a user control
        If Me.Parent.AcceptButton IsNot Nothing AndAlso _
              Me.Parent.AcceptButton.Parent Is Me Then
            ' the current default button on the parent form belongs to this
            ' user control, therefore we can proceed with the assignment
            Me.Command1.Default  = True
        End If

 

Previous | Index | Next