-
1
Activity #3 Working on C# Group Box, Radio Buttons & Check
Box
Objectives:
After completing this Activity you will be able to: o Use Visual
Studio to build simple Windows Forms applications. o Apply the use
Radio Buttons and Check Box in Application development. o Use
Conditional Statements in Manipulating the Event Controls of Radio
Buttons and
Check Box.
Introduction:
Group Box. C# windows control that displays a frame around a
group of controls with an optional caption.
Figure 3.1 Windows form with Group Box, Check Box and Radio
Button
Radio Button. Allows distinct selection of several items. In
Groups of Radio Buttons, only one can be selected (Checked). This
gives an exclusive selection for variety of items.
Check Box. Functions almost the same with Radio Buttons except
that using this controls you can allow multiple selections at
once.
-
2
Working on Group Box:
Group Box has essential functions of groupings your controls
together. It gives your form a much more
organize controls. Just like other controls you can change the
properties of your Group Box depending
on your needs. But most of the times you only access and change
the Name property and the Text
property (caption) of this controls.
Example Activity:
1. Locate the Groupbox control in the Toolbox on the left, under
Containers (or search on all
windows form tools box).
Figure 3.2 Groupbox on Visual Studio
2. Drag it to your form and resize it. Locate the Name
Properties and change it to
CheckboxGroup.
3. Locate the Text Properties and change it to Which Engineering
Program do you like in TIP?
4. Add another Group box and drag it just at the side of your
CheckboxGroup , name it
RadioButtonGroup.
Working on Check Box:
5. Edit the Text property of the RadioButtonGroup to And your
year Level is?
6. Now, Add 8 Checkbox es and change the Text properties of each
checkbox to Engineering
Program Offered by TIP.
Figure 3.3 TIP Engineering Program
-
3
7. You may change the name properties if each checked box, but
for this activity we will use the
default name.
Working on Radio Button:
8. Now add Radio Button on the other Group and put the Year
Level as Text property of each
Radiobutton.
9. Again you may change the Name Properties of each these Radio
Button, but for our activity we
were going to use the default name.
Producing Output, Using Conditional Statements.
10. Radio Button and checkbox has a lot of Properties, Events
and Methods that can be access
depending on the needs of the applications. But to show the
processing involved in the access
of these following controls, we will add a button to be clicked.
Once clicked, it will identify which
Engineering Program in TIP you like the most and which Year
Level you are. The program will
do this by identifying which on the Check Boxes are selected and
which on the radio buttons is
selected.
-To implement this we will be manipulating the Checked Event of
the Checkbox and Radio
Buttons.
Checked Properties Checked Properties
Check Box Selected (Boolean: TRUE) Not Selected (Boolean: False)
Radio Button Selected (Boolean: TRUE) Not Selected (Boolean:
False)
In case of Check Box, if that certain checkbox is checked
(selected) it returns a value of TRUE,
otherwise it returns FALSE. This goes the same with Radio
Buttons.
11. Add a button just like in Fig. 3.4. Change its Name Property
to btnExecute and Text property
to Execute.
-
4
Figure 3.4 Final Form
12. Add the following codes under click event of btnExecute
13. Run the code. Click Start button on visual Studio.
//code to handle Check box String programLike, Year; programLike
= ""; //Initialize with "" if (checkBox1.Checked) //CheckBox1 is
Checked or checkBox2.Checked == TRUE programLike = programLike +
"Civil Engineering "; if (checkBox2.Checked) programLike =
programLike + "Chemical Engineering "; if (checkBox3.Checked)
programLike = programLike + "Computer Engineering "; if
(checkBox4.Checked) programLike = programLike + "Electrical
Engineering "; if (checkBox5.Checked) programLike = programLike +
"Electronics Engineering "; if (checkBox6.Checked) programLike =
programLike + "Industrial Engineering "; if (checkBox7.Checked)
programLike = programLike + "Mechanical Engineering "; if
(checkBox8.Checked) programLike = programLike + "Marine
Engineeringg "; //code to handle Radio Button if
(radioButton1.Checked) Year = "First year"; else if
(radioButton2.Checked) Year = "Second year"; else if
(radioButton3.Checked) Year = "Third year"; else if
(radioButton4.Checked) Year = "Fourth year"; else if
(radioButton5.Checked) Year = "Fifth year"; else Year = "Not
Specified"; MessageBox.Show("You Like " + programLike + " and your
Year Level is " + Year,"Message");
-
5
EXERCISES:
Exercise 3.1. Create a C# Calculator that performs the selected
operation on Radio buttons.
Exercise 3.2. Create a simple 5 Multiple Questions Exam that
uses Checkbox for Choices. Choices
must contain at least 2 correct answers for each question. When
the Button Check is clicked it
will check the answers for each question and compute the grade
based on TIP grading system.
Exercise 3.3. Create a C# program that will ask the user with at
least 4 questions about the attributes
of a super hero and presents whose super hero he/she based on
the answers in the 4 questions.
You may change the subject. (E.g. from super heroes you may
change it to, characters in DOTA,
gods and goddesses, animals or objects). Use Radio Buttons and
Checkbox in the design of your
applications.
---------------------------------------------------------------------------------------------------------------------------------------
Note on Exercises:
A. Have this at the beginning of your program as comment:
1. Name of programmer
2. Date performed
3. Instructor
4. Program Description
B. Prepare a documentation w/ the ff:
1. Manual with front page
2. GUI and Name Properties
3. Source Code