How to change Form1 label.text when setting checkbox on form2?

I am very new to C # and am trying my first experiments with two different forms.

I would like to have your label1 and button1 on Form1, and flag1 on Form2.

button1 in Form1 opens Form2, and as soon as you set checkbox 1 to Form2, the text in label1 will change.

I think that this should be done with the help of events, but events are the only thing that still really confused me, so I assume that in essence this issue is more likely connected with the use of events. Which I also find terribly confusing if I look at it on MSDN and other sites.

Help will be greatly appreciated, it makes me feel very stupid.

+4
source share
4 answers

You can subscribe to the CheckedChanged event from a checkbox in an instance of Form2, directly from an instance of Form1. Inside Form1, before displaying Form2, subscribe to the CheckedChanged event of this flag

Form2 frm = new Form2(); frm.chkBox1.CheckedChanged += new EventHandler(this.ReceiveCheckedChanged); frm2.ShowDialog(); 

and then define in Form1 (this) the handler of the checkedChanged event raised in Form2

 private void ReceiveCheckedChanged(object sender, EventArgs e) { CheckBox chk = sender as CheckBox; if(chk.Checked) this.label1.Text = "Checked"; else this.label1.Text = "UnChecked"; } 

for this you need to change the Modifiers property on the checkbox from Private to Public

Thus, Form2 does not need to know that Form1 exists, and every time someone clicks on this checkbox, you need to change the shortcut in a different form. The responsibility for changing one’s internal state (text on the label) is on Form1, which notified the system of its requirements.

0
source

In this case, you can use the CheckedChanged event:

 public void checkbox2_CheckedChanged(object sender, EventArgs e) { if (checkbox2.Checked) { Form1.Label1.Text = "Checkbox 2 has been checked"; } else { Form1.Label1.Text = ""; } } 

http://www.c-sharpcorner.com/uploadfile/mahesh/checkbox-in-C-Sharp3/

Note that you will have to change the access modifier in Form1 and make Label1 public, so Form2 can change the Text property.

To do this, go to Form1 , select Label1 goto Properties , select Modifiers and change from Private to Public . Form 2 will have access to Label .

+2
source

Using a controller and events to decouple forms

The right way to do this is to separate the two forms from each other by introducing the Controller class and using events to change the state of the signal.

Here is an example.

First, create a new default Windows Forms application, WindowsFormsApplication1 and add two forms: form1 and form2 .

Then add to form1 button called "button1" and the label "label1".

Then add the checkbox1 flag to form2 .

In form1 designer form1 double-click the button to add a click handler for it.

In the form2 constructor, double-check the box to add a change handler for it.

Now add a new class called "Controller" and add the following code to it:

 using System; using System.Windows.Forms; namespace WindowsFormsApplication1 { internal sealed class Controller { public void RunForm1() { _form1 = new Form1(); // The next line defines our response to the button being pressed in Form1 _form1.ButtonClicked += (sender, args) => showForm2(); Application.Run(_form1); } private void showForm2() { var form2 = new Form2(); // The next line defines our response to the checkbox changing in Form2. form2.CheckBoxChanged += (sender, args) => _form1.SetLabel("Checkbox = " + ((CheckBox)sender).Checked); form2.ShowDialog(_form1); } private Form1 _form1 ; } } 

Now change Form1.cs to this:

 using System; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1: Form { // Here where we announce our event handler to the world: public event EventHandler ButtonClicked; public Form1() { InitializeComponent(); } public void SetLabel(string text) { label1.Text = text; } private void button1_Click(object sender, EventArgs e) { // We make a copy of ButtonClicked before checking it for null because // in a multithreaded environment some other thread could change it to null // just after we checked it for nullness but before we call it, which would // cause a null reference exception. // A copy cannot be changed by another thread, so that safe to use: var handler = ButtonClicked; if (handler != null) handler(sender, e); } } } 

And change Form2.cs to this:

 using System; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form2: Form { // Here the event handler for the check box: public event EventHandler CheckBoxChanged; public Form2() { InitializeComponent(); } private void checkBox1_CheckedChanged(object sender, EventArgs e) { var handler = CheckBoxChanged; if (handler != null) handler(sender, e); } } } 

Finally, change Program.cs to this:

 using System; using System.Windows.Forms; namespace WindowsFormsApplication1 { static class Program { [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Controller controller = new Controller(); controller.RunForm1(); } } } 

Now run the program and click the button, and then check the box several times. You will see a shortcut in Form1.

Thus, you completely separate form1 from form2 and put the control logic in a separate controller class.

+2
source

Form1:

 public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { var form = new Form2(); form.Changed += (o, args) => label1.Text = "some"; form.ShowDialog(); } } 

Form2:

 public partial class Form2 : Form { public delegate void ChangedEventHandler(object sender, EventArgs e); public event ChangedEventHandler Changed; public Form2() { InitializeComponent(); } private void checkBox1_CheckedChanged(object sender, EventArgs e) { if (Changed != null) { Changed(this, e); } } } 

Use the CheckedChanged event of the CheckBox.

In addition, you can view a good tutorial on how to use events in C #.

0
source

All Articles