how to know that check box is clicked

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rsbrothers
    New Member
    • May 2010
    • 2

    how to know that check box is clicked

    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.
  • ThatThatGuy
    Recognized Expert Contributor
    • Jul 2009
    • 453

    #2
    Originally posted by rsbrothers
    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
    loop through all checkboxes and check for a logical value i.e. True...
    in CheckBox.Checke d property

    Comment

    • rsbrothers
      New Member
      • May 2010
      • 2

      #3
      not working

      Comment

      • ThatThatGuy
        Recognized Expert Contributor
        • Jul 2009
        • 453

        #4
        Originally posted by rsbrothers
        not working
        show me your code... how have you worked so far

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          Originally posted by OriginalPoster
          Original 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?
          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.
          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.
          Code:
          bool IsOn = Form2.button1.IsChecked;
          Good: Use a property to get such information
          Code:
          //Form1
          bool IsOn = Form2.IsOn;
          Code:
          //Form 2
          public bool IsOn
          {
             get { return CheckBox1.Checked; }
             set { CheckBox1.Checked = value; }
          }
          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"

          Form2
          Code:
          public bool IsOn
          {
             get { return btnMeaningfulName.Checked; }
             set {
                      btnMeaningfulName.Checked = value;
                      panelDashboard.Visible = value;
                      labelStatus = value == true ? "On" : "Off";
                      btnRunNow.Enabled = value;
                 }
          }
          Now 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.

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            ThatGuy: loop through all checkboxes and check for a logical value i.e. True...in CheckBox.Checke d property
            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.

            Comment

            • Christian Binder
              Recognized Expert New Member
              • Jan 2008
              • 218

              #7
              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 helps

              Comment

              • ThatThatGuy
                Recognized Expert Contributor
                • Jul 2009
                • 453

                #8
                Originally posted by tlhintoq
                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.
                Its Not ThatGuy its ThatThatGuy....

                Comment

                • Curtis Rutland
                  Recognized Expert Specialist
                  • Apr 2008
                  • 3264

                  #9
                  Originally posted by ThatThatGuy
                  Its Not ThatGuy its ThatThatGuy....
                  Oh, That ThatThatGuy!

                  Comment

                  Working...