Using CheckBox In Windows Forms

INTRODUCTION

In this article, I am going to explain how to use a CheckBox in a Windows Forms app using Visual Studio 2017.

STEP 1 - Start the Project 

Let's create a new project using Visual Studio 2017.

Select New Project-->Visual C#-->Windows Forms App (.NET Framework), give your project a name and click OK.  

Using CheckBox In Windows Forms 

STEP 2 - Drag and Drop Control 
 
Let's add a CheckBox control to the form by dragging it from Toolbox and dropping it to the form. You will see that CheckBox 1 is added to the form. This control is now available to you in the code behind. 

CheckBox Control

CheckBox allows the user to make multiple selections from a provided options. It is used to give a user an option, such as true/false or yes/no. You can click a CheckBox to select it and click it again to deselect it. The CheckBox is a useful control in Windows Forms platform and the C# language. But it is not often a major focus of an application. It is a small, supporting control used with other controls. 

Using CheckBox In Windows Forms

Now, let's add another control to the form by dragging the other control from Toolbox to the form. You may also want to change the properties of the other controls. 
 
Let's add a button control to the form. CheckBox's state is read when a user performs an action on another control, such as a button. Change the text of button control to what you like.
 
STEP 3 - Coding for CheckBox Checked Event Handler

The CheckedChanged event occurs when the value of the Checked property changes. To add this event handler, you go to Events window and double click on the CheckedChanged event.
  1. private void checkBox1_CheckedChanged(object sender, EventArgs e)      
  2.        {      
  3.            Do_Checked();      
  4.        }      
  5.        private void Do_Checked()      
  6.        {      
  7.            button1.Enabled = checkBox1.Checked;      
  8.        }      
  9.       
  10.       private void Form1_Load(object sender, EventArgs e)      
  11.       {      
  12.           Do_Checked();     

STEP 4 - Compile and Run
 
Now, simply compile and run the application. 
 
If the CheckBox is Checked, then the button click is enabled and if the CheckBox is not checked, then the button click is disabled.

Using CheckBox In Windows Forms 
 
Using CheckBox In Windows Forms 

Summary
 
In this basic article, you saw how to use a CheckBox control to enable and disable button control. Hope you found this article interesting. For any feedback, please post it as a comment at the bottom of this article. Thank you!


Similar Articles