- 1 year ago
Checkbox (checked or unchecked)
To work with checkboxes in a Windows Forms application using C#, you can use the CheckBox
control provided by the System.Windows.Forms
namespace. Here's an example of how to determine if a checkbox is checked or unchecked:
Add a
CheckBox
control to your form:- Open your Windows Forms designer in Visual Studio.
- Drag and drop a
CheckBox
control from the Toolbox onto your form.
Access the checkbox state:
- By default, the
CheckBox
control has a property namedChecked
that represents its current state. It is a boolean property that returnstrue
if the checkbox is checked andfalse
if it is unchecked. - You can access the
Checked
property to determine the checkbox state in your code.
Here's an example that demonstrates how to determine the checkbox state when a button is clicked:
csharpprivate void button1_Click(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
// Checkbox is checked
MessageBox.Show("Checkbox is checked.");
}
else
{
// Checkbox is unchecked
MessageBox.Show("Checkbox is unchecked.");
}
}
In this code, we handle the
Click
event of a button (button1
) and check theChecked
property of theCheckBox
control (checkBox1
) to determine its state. Based on the state, we display a corresponding message usingMessageBox.Show()
.- By default, the
By using the Checked
property of the CheckBox
control, you can determine if a checkbox is checked or unchecked in your Windows Forms application.