I am trying to pass a CheckBox value from UserControl3 to UserControl1
On UserControl3
public void materialCheckBox1_CheckedChanged(object sender, Ev
Work with delegates!
To read more about that click here! For more check this msdn article out.
There are multiple solutions for communication between controls.
You have seen such functionality in interaction between controls like BindingNavigator
and Bindingource
where the BindingNavigator
has a property of type BindingSource
and each time you click on navigation buttons, the BindingNavigator
calls methods of BindingSource
.
To implemenet it for yourself, for example in UserControl2
you can create a public property exposing the information which you want the UserControl1
be able to check, then in the UserControl1
, you should have a property of type of UserControl2
. This way when you assign the instance of UserControl2
to the property at design-time or run-time, then you can use exposed information.
For example follow these steps:
1) In your UserControl2
, expose the information which you need to use outside of control.
public bool CheckBoxValue
{
get { return checkBox1.Checked; }
set { checkBox1.Checked = value; }
}
2) In your UserControl1
, create a property of type UserControl2
. So you can use the instance which is assigned to it and find the value of CheckBoxValue
property.
public UserControl2 UserControl2Instance { get; set; }
private void button1_Click(object sender, EventArgs e)
{
if(UserControl2Instance!=null)
{
if(UserControl2Instance.CheckBoxValue)
MessageBox.Show("Checked");
else
MessageBox.Show("Unchecked");
}
}
3) Drop both UserControl1
and UserControl2
on the form and using designer (or at run-time) assign the instance of UserControl2
to UserControl2Instance
property of UserControl1
. Then when you run the program and click on Button1
of your UserControl1
, you can see the value of checkBox1
which is located on UserControl2
.