I have 6 check boxes in parent windows form and 6 labels in child windows form. If I click the checkbox1 in parent windows form I want that checkbox1 is clicked message in child windows form. Like that if all the checkboxes are clicked then i want checkbox1,check box2,....checkb ox6 clicked in child windows form.
how to know that check box is clicked
Collapse
X
-
Tags: None
-
loop through all checkboxes and check for a logical value i.e. True...hi,
i have 6 check boxes in parent windows form and 6 labels in child windows form.
now the question is if i click the checkbox1 in parent windows form i want that checkbox1 is clicked message in child windows form.like that if all the checkboxes are clicked then i want checkbox1,check box2,....checkb ox6 clicked in child windows form.
please help
i want code in c#
thank you
in CheckBox.Checke d property -
-
Comment
-
Although you can have Form1 directly access items on Form2 it isn't the recommended way to go. It ties the two forms tightly to each other. If a change is made in Form2 such as removing one of the controls, then code in Form1 breaks.Originally posted by OriginalPosterOriginal Poster: How do I get my Form2 to react to something on my Form1?
How do I make my Form1 control something on my Form2?
It is better to Form1 raise an event, and have Form2 contain its own code for how to react to this. This places responsibility for Form2 within Form2, and Form1 within Form1.
It keeps Form1 blissfully ignorant of Form2 - and a logging component - and a progress component, and a dozen other little black boxes that can be subscribed to events in Form1, all without Form1 being made responsible for directly affecting controls other than itself.
Events tutorial (including Form to Form which is the same as class to class)
This tutorial for a cash register does exactly that: It makes a virtual numeric keyboard.
Bad: Directly accessing controls of one class/form from another.Good: Use a property to get such informationCode:bool IsOn = Form2.button1.IsChecked;
Code://Form1 bool IsOn = Form2.IsOn;
It's a subtle but important difference as your applications become more complex. Using properties means your target class/form (Form2) can be changed and updated a thousand different ways yet won't negatively impact the classes that are reading from it. If you change the name of a control for example: It won't break references in all your other classes. If your target form evolves where it needs to do 10 things when it is turned on, then the responsibility stays within Form2, and that burden is not put on Form1 and a dozen other forms that might be using Form2. The goal is to compartimentali ze the work so that Form2 is responsiblity SOLELY for Form2. From1 should only have to say "Turn on" or "Turn Off"Code://Form 2 public bool IsOn { get { return CheckBox1.Checked; } set { CheckBox1.Checked = value; } }
Form2Now when Form1 tells Form2 to turn on, Form2 will check a box, make an entire panel visible, change its Status label to say 'On' and enable a Run Now button.Code:public bool IsOn { get { return btnMeaningfulName.Checked; } set { btnMeaningfulName.Checked = value; panelDashboard.Visible = value; labelStatus = value == true ? "On" : "Off"; btnRunNow.Enabled = value; } }Comment
-
Please read the O.P's question again. He needs an event-driven solution. We wants to *react* to the checkchanged event of a checkbox. Not add a method that must be activated separately to check *all* the boxes.ThatGuy: loop through all checkboxes and check for a logical value i.e. True...in CheckBox.Checke d propertyComment
-
I would attach to each CheckBox's CheckedChanged event with the same method for each CheckBox.
E.g. chk1.CheckedCha nged += AnyCheckBox_Che ckedChanged;
Then I would implement an event on my parent-form named AnyCheckBoxChec kedChanged; (I would do it without data, but you can also pass data with this event)
Further I's make a readonly property on parent-form with type of IEnumerable<Che ckBox> which returns all the checked CheckBoxes.
In my child-form I'd attach to this event and every time it is fired, ask the parent for the checked CheckBoxes und do whatever I want to do with it e.g. display their names.
Hope this helpsComment
-
Comment
-
Comment
Comment