Please guide me as how to uncheck a checkbox of a parent form from a child form in Visual studio C#?
Its like when a button in child form is pressed/clicked, then a checkbox in parent form should be unchecked.
开发者_运维知识库Also I am using WindowsForm.
Please guide
Regards
Asad
So, I see some... not so good answers telling you to go ahead and make your checkbox public. Don't. Expose an event from your MDI forms that the main form can handle as they are created. When the child form fires the event the main form can update the UI as it sees fit.
It's really not a good idea to expose your UI elements. What if you remove that CheckBox or want to use a different control in its stead? Now you have to go and modify your child forms as well because they expect that CheckBox to be there. You are leaking implementation details and that really just makes things more complicated down the road.
How to do this? Please guide
I would be happy to, especially when I consider the answer you chose as "correct", which is honestly plain awful. Here is a simple code sample that defines a Form class which exposes an event. When that event is handled by the parent form, the UI is updated.
public class ChildForm
{
public event EventHandler SomeEvent;
protected virtual void OnSomeEvent( EventArgs e )
{
EventHandler del = SomeEvent;
if( del != null )
{
// fire the event
del( this, e );
}
}
private void someButton_Click( object sender, EventArgs e )
{
// for the sake of example, let's assume that you want
// to notify listeners of "SomeEvent" when a button is clicked
OnSomeEvent( this, EventArgs.Empty );
}
}
public class MainForm : Form
{
private void ShowChildForm( )
{
using( ChildForm frm = new ChildForm() )
{
frm.SomeEvent += frm_SomeEvent;
frm.ShowDialog();
}
}
private void frm_SomeEvent( object sender, EventArgs e )
{
// this is where we handle the event "SomeEvent" that the
// child form fires when you need to communicate that something has happened.
// now you may update the UI as needed and the ChildForm class does not have
// to know anything about how the UI is actually implemented.
// this is referred to as "loose coupling" and makes your code more maintainable.
someCheckbox.Checked = true;
}
}
It would be best practice not to have a direct reference/dependency from your child form to your parent form. Your child form should not have the responsibility to uncheck the checkbox on your parent form or even know of the existence of your parent form (se the Single responsibility principle).
One way to obtain this is to use the Observer pattern by using an event on the child form which the parent form can listen to. Here is a event tutorial: http://msdn.microsoft.com/en-us/library/aa645739(VS.71).aspx.
A code example for doing this (only partial code shown):
//Parent form.
public partial class Form1 : Form
{
//This is the checkbox on your parent form
CheckBox myCheckBox = new CheckBox();
public Form1()
{
InitializeComponent();
Form2 form2 = new Form2();
form2.MyUncheckingEvent += new EventHandler(form2_MyUncheckingEvent);
}
void form2_MyUncheckingEvent(object sender, EventArgs e)
{
myCheckBox.Checked = false;
}
}
//Child form
public partial class Form2 : Form
{
public event EventHandler MyUncheckingEvent;
public Form2()
{
InitializeComponent();
}
public void MethodFormRaisingTheEvent()
{
if (MyUncheckingEvent != null)
{
MyUncheckingEvent(this, EventArgs.Empty);
}
}
}
see my question get-back-hidden-form-from-another-form
in addition to that you have to implement like this:
//Form2
private Form refToForm1;
public Form RefToForm1
{
get { return refToForm1; }
set { refToForm1 = value; }
}
// On buttonClick
CheckBox cb=(CheckBox)this.RefToForm1.Controls["checkBox1"];
cb.Checked = !cb.Checked;
//Form1
Form2 obj2 = new Form2();
obj2.RefToForm1 = this;
obj2.Show();
Cast the Parent
property into the main form's class type.
For example, in the context of the child form:
(this.Parent as frmMain).MyCheckBox.Checked = false;
精彩评论