Passing Data Between Forms

A couple of approaches:

1 – Store the Form1 variable “setDateBox” as a class member of Form3 and then access the “setNoDate” method from the checkboxes CheckedChanged event handler:

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    setDateBox.setNoDate(checkBox1.Checked);
}

2 – If you do not wish to store setDateBox as a class member or you need to update more than one form, you could Define an event within Form3 which like so:

public event EventHandler<CheckedChangedEventArgs> CheckBox1CheckedChanged;

...

public class CheckedChangedEventArgs : EventArgs
{
    public bool CheckedState { get; set; }

    public CheckedChangedEventArgs(bool state)
    {
        CheckedState = state;
    }
}

Create a handler for the event in Form1:

public void Form1_CheckBox1CheckedChanged(object sender, CheckedChangedEventArgs e)
{
    //Do something with the CheckedState
    MessageBox.Show(e.CheckedState.ToString());
}

Assign the event handler after creating the form:

Form1 setDateBox = new Form1();
CheckBox1CheckedChanged += new EventHandler<CheckedChangedEventArgs>(setDateBox.Form1_CheckBox1CheckedChanged);

And then fire the event from Form3 (upon the checkbox’s checked state changing):

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    if(CheckBox1CheckedChanged != null)
        CheckBox1CheckedChanged(this, new CheckedChangedEventArgs(checkBox1.Checked));
}

Hope this helps.

Leave a Comment