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.
checkBox1
is a member of Form3
, so from Form1
you cannot reference it that way.
You could:
- create a separate class you share amongst your forms that keeps values that affect the whole application
- make
Form3.checkBox1
publically visible, so you can reference it bymyForm3Instance.checkBox1
In the designer of the form containing checkbox, Make it to internal or public. Then you can access the control from the forms object. Its a quick and dirty way to achieve but it might solve your issue.
ex
In form1.designer.cs
existing
private CheckBox checkbox1;
new one
internal CheckBox checkbox1; or
public CheckBox checkbox1;
You are creating a new instance of Form1 and not referencing the existing instance of it.
Form1 setDateBox = (Form1)this.Owner
That should fix your problem.