Search code examples
vb.nettextcheckboxlabelgroupbox

Why does the GroupBox's Text display as expected, but not the Labels' Text?


I have this form that contains a GroupBox, and in this GroupBox are some Labels and a CheckBox. The CheckBox is set to 'Checked' in design-time, which means when it loads that defaults to 'Checked'. Hence when a new instance of this form is created, the CheckChanged event for that check box fires before the Form's Load event. The Text values of the labels depend on whether the check box is checked or not.

This is in the CheckChanged event for the check box, which fires before the Form's Load event:

If chkUseAsFull.Checked Then
    fraHalf.Text = "Full Spacing"
    lblLitHalfSpacing.Text = "Spacing"
    lblLitHalfSpeed.Text = "Speed"
    txtHalfSpacing.Text = txtSpacing.Text
    txtHalfSpacing.Enabled = False
    cmdAdjustHalf.Enabled = False
Else
    fraHalf.Text = "Half Spacing"
    lblLitHalfSpacing.Text = "1/2 Spacing"
    lblLitHalfSpeed.Text = "1/2 Speed"
    txtHalfSpacing.Text = Format(spc, "##0.00")
    txtHalfSpacing.Enabled = True
    cmdAdjustHalf.Enabled = True
End If
  • fraHalf is the group box, and lblLitHalfSpacing and lblLitHalfSpeed are labels within it.

Note that in design-time, the Text values of the group box and labels are those specified when the check box is NOT checked.

But when I launch this form, the group box's Text is "Full Spacing" but the labels' Text is "1/2 Spacing" and "1/2 Speed".

Why would the text of the group box show according to the check box's checked value correctly, but not the text of those labels?


Solution

  • Don't rely on the designer to run code for you. Chances are the labels are changing, but then the designer is probably changing them back to their original properties later on in the code. Which means that if you know the default is checked, then the labels should be created in that default mode. Or in the load event, you can just call it yourself

    fraHalf_CheckChanged(fraHalf, EventArgs.Empty)