Previous | Index | Next 

[INFO] .NET behaves differently from VB6 if you disable a checked OptionButton control in Form_Load event handler

We have noticed a minor difference in how VB6 and .NET behave if the Form_Load event handler contains these statements:

Option1.Value = True
Option1.Enabled = False

Under VB6, the above statement cause the enabled OptionButton control with the lowest TabIndex value to become checked (i.e. Value=True), whereas under .NET the Option1 control will be checked even if it is disabled.

If your migrated code relies on the VB6 behavior, the only way to have it work correctly is emulating such a behavior, which you can do by means of the following method:

Public Sub SetCheckedOptionButton(container As Control)
   ' exit if wrong input
   If container Is Nothing Then Return
 
   Dim nextOptionButton As VB6OptionButton
 
   For Each ctrl As Control In container.Controls
      Dim tempOptionButton As VB6OptionButton = TryCast(ctrl, VB6OptionButton)
      If tempOptionButton IsNot Nothing Then
         ' verify the value
         If tempOptionButton.Checked Then
            ' the control is checked and enabled ... all ok.
            If tempOptionButton.Enabled Then Return
         Else
            ' verify the enable, one of this could became the new checked one
            If tempOptionButton.Enabled Then
               ' the first found
               If nextOptionButton Is Nothing Then
                  nextOptionButton = tempOptionButton
               Else
                  ' select the one with the minimum tabindex
                  If nextOptionButton.TabIndex > tempOptionButton.TabIndex Then
                     nextOptionButton = tempOptionButton
                  End If
               End If
            End If
         End If
      End If
   Next
 
   ' if we have found something... set the value
   If nextOptionButton IsNot Nothing Then nextOptionButton.Checked = True
End Sub

The SetCheckOptionButton is meant to be invoked from inside the Form_Load event handler, or immediately after you change the Enabled or Value properties of one or more OptionButton controls in a group.

 

Previous | Index | Next