I am using 2 checkboxes on WinForms.

I am trying to say to the program, if the box is checked.. do this..

If the box is unchecked..do that..

I know how to make it do what i want when the button is checked, but i dont know how to make it do something when the button is unchecked.

Here some of my code:

if (this.ChckBxAND1.Checked && this.ChckBxAND2.Checked)
            {
                LblResultAND.Text = "ON!";
            }

So, when it is checked, it outputs the the Lbl the text "ON!"

if (//the checkbox is not checked)
            {
                LblResultAND.Text = "OFF!"; //do the following
            }

Much Appreciated!

Regards

To check if a CheckBox isn't checked use:

if(ChckBxAND1.Checked == false) { ..... }

or use:

if(!ChckBxAND1.Checked) { ..... }
Comments
thank you

Thank you, that works!

Can I ask though, how do i make the program constantly read whether the checkboxes has been changed? Because when I load the Form, it automatically gives me a reading of "OFF" which is correct, but when I change the check state of the btns, it does not change the value held in the Lbl.

Do you know how to make it recognize that is has been changed please? ill show you my coding I have at present:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CamShapes
{
    public partial class LogicGates : Form
    {
        public LogicGates()
        {
            InitializeComponent();
        }
        
        //AND GATE LOGIC        
         
        private void LogicGates_Load(object sender, EventArgs e)
        {
            if (ChckBxAND1.Checked && ChckBxAND2.Checked) // If both are checked..
            {
                LblResultAND.Text = "ON!";
            }
            if (this.ChckBxAND1.Checked && !ChckBxAND2.Checked) // If the first on is checked, but the second one is not..
            {
                LblResultAND.Text = "OFF!"; // Do this..
            }
            if (!this.ChckBxAND1.Checked && this.ChckBxAND2.Checked) // If the first one is not checked, but the second one is..
            {
                LblResultAND.Text = "OFF!";// Do this..
            }
            if (!this.ChckBxAND1.Checked && !this.ChckBxAND2.Checked) // If both of them are not checked..
            {
                LblResultAND.Text = "OFF!";// Do this..
            }            
        }
    }
}

much appreciated!

Go to DesignMode on your Form, double click your CheckBox control, by now you'll see that Visual Studio created a new method:

private void ChckBxAND1_CheckedChanged(object sender, EventArgs e)
{
}

This is a Event, don't know if you heard about that; So the program runs the code you have in that function everytime you change the state of the checkbox.
Do this to both of your checkboxes and create a new method and call it from both functions you've created by doing what I mencioned above. In that function place the code you have on LogicGates_Load(object sender, EventArgs e) method.

It will become something like:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CamShapes
{
    public partial class LogicGates : Form
    {
        public LogicGates()
        {
            InitializeComponent();
        }
        
        //AND GATE LOGIC        
         
        private void LogicGates_Load(object sender, EventArgs e)
        {
            // Do other stuff you may want to;            
        }
		
	private void ChckBxAND1_CheckedChanged(object sender, EventArgs e)
        {
	    checkState();
        }
		
	private void ChckBxAND2_CheckedChanged(object sender, EventArgs e)
        {
            checkState();
        }
		
	private void checkState()
	{
	    if (ChckBxAND1.Checked && ChckBxAND2.Checked) // If both are checked..
            {
                LblResultAND.Text = "ON!";
            }

            if (this.ChckBxAND1.Checked && !ChckBxAND2.Checked) // If the first on is checked, but the second one is not..
            {
                LblResultAND.Text = "OFF!"; // Do this..
            }

            if (!this.ChckBxAND1.Checked && this.ChckBxAND2.Checked) // If the first one is not checked, but the second one is..
            {
                LblResultAND.Text = "OFF!";// Do this..
            }

            if (!this.ChckBxAND1.Checked && !this.ChckBxAND2.Checked) // If both of them are not checked..
            {
                LblResultAND.Text = "OFF!";// Do this..
            }
	}
		
    }
}

Hope it helps!

And one other thing, I noticed now that the state is only ON if both checkboxes are checked, so there's no need for you to use that many If statements on the tests, use:

if (ChckBxAND1.Checked && ChckBxAND2.Checked) // If both are checked..
{
    LblResultAND.Text = "ON!";
}
else // At least one is not checked...
{
    LblResultAND.Text = "OFF!";
}

That will equally do the trick for you! ;)

Comments
Good thinking.

alright thank you extremely much, i will try that tonight!

thank you once again!

whoops, sorry, nevermind I sorted it ;)

Edited 5 Years Ago by simagen: n/a

This article has been dead for over six months. Start a new discussion instead.