Forms in Visual Basic


1. Build FrmDays as shown. The form has:
CheckboxesCheckbox : ChkMonday, ChkTuesday, ChkWednesday, ChkThursday, and ChkFriday. However, the names do not matter and you can add Saturday and Sunday without changing the code that follows because we will be using the forms collection to process the results.
A label : LblPrompt
A button : BtnOk

2. Write the code for btnOk:
Private Sub BtnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
   Handles BtnOK.Click
  Me.Hide()
End Sub 'BtnOK_Click

This is all of the code for FrmDays. We will now go back to FrmMain and write the code for BtnDays. As we write the code for FrmMain it is important to remember that FrmDays is just a definition of a form object. We will create an instance of the form in order to display it.

Private Sub BtnDays_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
  Handles BtnDays.Click
  Dim Ctrl As New Control() 'to loop through the controls
  Dim Chk As New CheckBox() 'in case it is a checkbox
  Dim Frm As New FrmDays() 'create an instance of the form
  Dim S As String = ""
  Frm.ShowDialog() 'program waits for form to be closed
  For Each Ctrl In Frm.Controls
     If (TypeOf Ctrl Is CheckBox) Then
        Chk = Ctrl 'to access properties of a checkbox
        If Chk.CheckState = CheckState.Checked Then
           S = Ctrl.Text & " " & S & " "
        End If
     End If
  Next Ctrl
  Me.LblDays.Text = S
End Sub 'BtnDays_Click
Run the program and click the Get Days button. The instance of FrmDays appears. Check a couple of days, then click OK. After you click Ok. The form closes and the names selected appear in the label of form main.

NEXT: Show Modal