AllTheTimeWorld.com

csharp formsUsing the custom form.

Using the custom form.

A Form is shown modal, when closed selections are processed

Using the custom form.

A Form is shown modal, when closed selections are processed

Build FormDays as shown.


The form has: The form has: CheckBoxesCheckbox : chkDay0 chkDay1, chkDay2, chkDay3, and chkDay4. 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 of controls to process the results.
A label : lblInstructions
A button : btnOk

2. Write the code for btnOk:
private void btnOK_Click(object sender, EventArgs e)
{
  this.Hide();
}

This is all of the code for FormDays. We will now go back to Form1 and write the code for mnuGetDays. 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 void mnuGetDays_Click(object sender, EventArgs e)
{
    // Create a new instance of FormDays.
    FormDays frm = new FormDays();
    // Display the form modal.
    frm.ShowDialog();
    // This code executes after the user has selected days and pressed OK.
    String s = "";
    // The controls on frm include checkboxes, a label and a button.
    // Look at each control...
    foreach (Control ctrl in frm.Controls)
    {
       // If the control is a check box ...
       if (ctrl is CheckBox)
       {
          // Cast the control ctrl to a checkbox chk.
          CheckBox chk = (CheckBox)ctrl;
          // If the checkbox is checked add the text to the string.
          if (chk.Checked) s += chk.Text + " ";
       }
    }
    // Display the days selected in the label.
    lblDays.Text = s;
}
Run the program and click the Get Days menu item.
The instance of FormDays 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.