none
how to detect if checkbox was checked??

    Question

  • what will the code be for this?

    if (checkbox1.????) i have in my intellisense list checkstatechanged but i get error to say it must be on the left of the += 

    thanks
    Wednesday, January 27, 2010 1:16 PM

Answers

  • hi,

         
            private void frmMajorGroups_Load(object sender, EventArgs e)
            {
                // TODO: This line of code loads data into the 'majorGroupsData.MAJORGROUPS' table. You can move, or remove it, as needed.
                this.MajorGroupsTableAdapter.Fill(this.majorGroupsData.MAJORGROUPS);
    
                //checkEditShowInRest.Properties.ValueChecked.ToString();
                //checkEditShowInRest.Properties.ValueUnchecked.ToString();
                btnOk.Enabled = false;
                this.checkEditShowInRest.CheckedChanged += new System.EventHandler(this.checkEditShowInRest_CheckChanged);
            }
    
            private void checkEditShowInRest_CheckChanged(object sender, EventArgs e)
            {
               CheckBox chk=(CheckBox)sender;
               if(chk.Checked)
                    btnOk.Enabled = true;
              else
                   btnOk.Enabled = false;
            }
    
    This code is as i understood, if the checkbox is checked then bthOK button should get enabled to true else make it false. if any changes feel free to ask............

    Nagarjuna Dilip
    • Marked as answer by helloise Thursday, January 28, 2010 12:15 PM
    Thursday, January 28, 2010 9:36 AM

All replies

  • this.checkBox1.CheckStateChanged += new System.EventHandler(this.checkBox1_CheckStateChanged);

    And this later in your class you implement the method:

            private void checkBox1_CheckStateChanged(object sender, EventArgs e)
            {
    
            }
    Wednesday, January 27, 2010 1:29 PM
  • If you just want to test for checked/unchecked then you probably want:  if (checkbox1.Checked)

    Wednesday, January 27, 2010 4:49 PM
  • Maybe this:
    if (checkBox1.Checked) //If checkBox1.Checked is true (which means is checked), then MessageBox.Show()
                           //will be invoked.
      MessageBox.Show("The checkbox is checked!");  


    Another thing you can do is:

            private void checkBox1_CheckedChanged(object sender, EventArgs e)

            {

                CheckBox cb = sender as CheckBox;

                label1.Text = cb.Checked == true ? "The " + cb.Text : "Nothing selected!";

            }

    I hope this helps...

    Have a nice day...

    Best regards,
    Fisnik


    Coder24.com
    • Proposed as answer by Raunak Jhawar Thursday, January 28, 2010 7:46 AM
    Wednesday, January 27, 2010 5:12 PM
  • private void checkBox1_CheckedChanged(object sender, EventArgs e) 
        { 
             if (checkBox1.Checked) 
             MessageBox.Show("checked."); 
        }

    Thursday, January 28, 2010 7:41 AM
  • Hi,
    hope it helps
      private void checkBox1_CheckedChanged(object sender, EventArgs e)
            {
                if (checkBox1.CheckState.Equals(CheckState.Checked))
                {
                   
                }
            }

    Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
    Thursday, January 28, 2010 8:24 AM
  • thanks all for the answers, but there is a bit of a snag... i have a datagrid on the left and then the checkbox on the right....depending on which line i am on, the checkbox will be checked or not....i also have a ok button which must be  GREYED OUT until i check or uncheck the checkbox

    my code:
     private void frmMajorGroups_Load(object sender, EventArgs e)
            {
                // TODO: This line of code loads data into the 'majorGroupsData.MAJORGROUPS' table. You can move, or remove it, as needed.
                this.MajorGroupsTableAdapter.Fill(this.majorGroupsData.MAJORGROUPS);

                checkEditShowInRest.Properties.ValueChecked.ToString();
                checkEditShowInRest.Properties.ValueUnchecked.ToString();
                btnOk.Enabled = false;
                this.checkEditShowInRest.CheckedChanged += new System.EventHandler(this.checkEditShowInRest_CheckChanged);
            }
            private void checkEditShowInRest_CheckChanged(object sender, EventArgs e)
            {
                btnOk.Enabled = true;
            }

    but now if i scrolll trhough the datagrid the ok button becomes available because the checkstate changes from one row to the next, and that is NOT how i want it... i only want ok button enabled when there is a change to the checkbox for a particular row...

    hope it makes sense!
    thanks


    Thursday, January 28, 2010 8:28 AM
  • Hi,
    hope it helps
     private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
            {
                if (dataGridView1.CurrentCell is DataGridViewCheckBoxCell && dataGridView1.CurrentCell.Value.Equals(true))
                {
                    button1.Enabled = true;
                }
            }
    
            private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
            {
                if (dataGridView1.CurrentCell is DataGridViewCheckBoxCell)
                {
                    dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
                }
    
            }

    Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
    Thursday, January 28, 2010 9:35 AM
  • hi,

         
            private void frmMajorGroups_Load(object sender, EventArgs e)
            {
                // TODO: This line of code loads data into the 'majorGroupsData.MAJORGROUPS' table. You can move, or remove it, as needed.
                this.MajorGroupsTableAdapter.Fill(this.majorGroupsData.MAJORGROUPS);
    
                //checkEditShowInRest.Properties.ValueChecked.ToString();
                //checkEditShowInRest.Properties.ValueUnchecked.ToString();
                btnOk.Enabled = false;
                this.checkEditShowInRest.CheckedChanged += new System.EventHandler(this.checkEditShowInRest_CheckChanged);
            }
    
            private void checkEditShowInRest_CheckChanged(object sender, EventArgs e)
            {
               CheckBox chk=(CheckBox)sender;
               if(chk.Checked)
                    btnOk.Enabled = true;
              else
                   btnOk.Enabled = false;
            }
    
    This code is as i understood, if the checkbox is checked then bthOK button should get enabled to true else make it false. if any changes feel free to ask............

    Nagarjuna Dilip
    • Marked as answer by helloise Thursday, January 28, 2010 12:15 PM
    Thursday, January 28, 2010 9:36 AM
  • thanks nagarjuna...

    just one thing...with the checkchanged property there is also a checkStateChanged property ..

    what is the difference please???
    Thursday, January 28, 2010 12:17 PM
  • hi,

            private void checkBox1_CheckedChanged(object sender, EventArgs e)
            {
                MessageBox.Show("Check Change");
            }
    
            private void checkBox1_CheckStateChanged(object sender, EventArgs e)
            {
                MessageBox.Show("Check State Changed");
            }
    
            private void checkBox1_Click(object sender, EventArgs e)
            {
                MessageBox.Show("Click");
            }
    first checkchanged event fires then check state changed then click event fires.......

    Have a nice day


    Nagarjuna Dilip
    Friday, January 29, 2010 5:53 AM
  • thanks but still dont understand yet...when do you use which one of the three? or can you use any one for checking if the state changed?
    Friday, January 29, 2010 9:34 AM
  • hi,

    yes, use  check state changed event...............
    Nagarjuna Dilip
    Friday, January 29, 2010 10:22 AM
  • The difference between the two events is the following:

    CheckedChanged: fires whenever the value of the CheckBox.Checked property changes.
    CheckStateChanged: fires whenever the value of the CheckBox.CheckState property changes.

    This might seem the same thing, and usually it is, but you must remember that a CheckBox is intrinsically a three-state control that can assume the states "checked", "unchecked" and "indeterminate".

    Now, since CheckBox.Checked returns true when CheckBox.CheckState is either CheckState.Checked or CheckState.Indeterminate, the CheckedChanged event will not fire for transitions from CheckState.Checked to CheckState.Indeterminate or vice versa.
    Conversely, CheckStateChanged always fires when the value of CheckBox.CheckState changes.

    Just for completeness: you can always set CheckBox.CheckState to CheckState.Indeterminate via code, but your users won't be able to do so directly unless you set CheckBox.ThreeState to true.

    HTH
    --mc

    Friday, January 29, 2010 9:16 PM