Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If one checkbox is checked, set the other to unchecked

I have two checkboxes on my form; chkBuried and chkAboveGround. I want to set it up so if one is checked, the other is unchecked. How can I do this?

I have tried the CheckChanged property:

private void chkBuried_CheckedChanged(object sender, EventArgs e)
{
    chkAboveGround.Checked = false;
}
private void chkAboveGround_CheckedChanged(object sender, EventArgs e)
{
    chkBuried.Checked = false;
}

And it works, just not as well as I hoped. That is, when I check chkBuried, then check chkAboveGround, both boxes become unchecked before I can check another one again.

like image 872
Ben Avatar asked Mar 13 '14 02:03

Ben


People also ask

How do you uncheck a checkbox when another one is checked in react?

Here's one approach: import React from "react"; import ReactDOM from "react-dom"; class App extends React. Component { // some example state state = { a: { curr: false, prev: false, disabled: false }, b: { curr: false, prev: false, disabled: false }, c: false }; toggleOthers = () => { if (this.

What is the value of checkbox when unchecked?

Note: If a checkbox is unchecked when its form is submitted, there is no value submitted to the server to represent its unchecked state (e.g. value=unchecked ); the value is not submitted to the server at all.

How do you check if a checkbox is checked or unchecked?

prop() and is() method are the two way by which we can check whether a checkbox is checked in jQuery or not. prop(): This method provides an simple way to track down the status of checkboxes. It works well in every condition because every checkbox has checked property which specifies its checked or unchecked status.


1 Answers

modify your code as below.

private void chkBuried_CheckedChanged(object sender, EventArgs e)
{
    chkAboveGround.Checked = !chkBuried.Checked;
}
private void chkAboveGround_CheckedChanged(object sender, EventArgs e)
{
    chkBuried.Checked = !chkAboveGround.Checked;
}
like image 176
Riz Avatar answered Oct 02 '22 21:10

Riz