Zebra0.com

csharp forms


Text of video

Method 1:

public partial class FormDays : Form
{
    public string days = "";
    ...
    
private void btnOK_Click(object sender, EventArgs e)
{
   // Set the public string days.
   if (chkDay0.Checked) days += chkDay0.Text + " ";
   if (chkDay1.Checked) days += chkDay1.Text + " ";
   if (chkDay2.Checked) days += chkDay2.Text + " ";
   if (chkDay3.Checked) days += chkDay3.Text + " ";
   if (chkDay4.Checked) days += chkDay4.Text + " ";
   this.Hide();
}

Method 2:

private void btnOK_Click(object sender, EventArgs e)
{
    this.Hide();
}
public string GetDays()
{
    string s = "";
    CheckBox[] chks = { chkDay0, chkDay1, chkDay2, chkDay3, chkDay4 };
    for (int i = 0; i < 5; i++) {
       if (chks[ i].Checked) s += chks[i].Text+" ";
     }
    return s;
}

On form 1 your would have:

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.
    // The public variable days is computed when the user presses OK on form days.
// Use either: lblDays.Text = frm.days; // Or the Second method: The public function GetDays is called. lblDays.Text = frm.GetDays(); }
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.

End of lesson, Next lesson: