Example 8:
Design and develop an application system that disables or enables a Button, and displays its
feedback to the Textbox. When the user clicks the Try to Click Me! Button, the feedback that
says ‘Yes! I was Enabled! Thank you!” should be displayed at the Text box. Now when the user
clicks the Disable Button! Radio button, the Button control should be displayed (graved), and
the feedback that says “You Disabled the Button!” is displayed at the Text box. Plus, the Try to
Click Me! Button is unclickable. Now when the user clicks the Enable Button! Radio button, the
Try to Click Me! Button should be restored to its enables state, and the feedback that says “You
Enabled the Button!” should be displayed at the Text box. Follow the given figure below in
designing and developing the application system.
vbDisableControl1
Try to Click Me! Button
Text box
Enable Button! Disable Button! Radio Buttons
Figure 2.7 Disabling and Enabling Controls
Solution:
1. Start Visual Basic 2008 by clicking Start, All Programs, and Microsoft Visual Basic 2008 at the
Windows Vista operating system.
2. Create a new Windows Forms Application project by using either the file menu or the New
Project icon in the toolbar
3. Name the new application system vbDisableControl1.
4. Set the Form’s Text property to vbDisableControl1, too.
5. Click, drag and drop a button from the Toolbox to add it into the form, and set its Text property
to Try to Click Me!.
6. Click, drag and drop a Text box from the Toolbox to add it into the Form.
7. Click, drag and drop two (2) Radio buttons from the Toolbox to add them into the form, then set
their respective Text property to enable Button!, and disable Button!.
8. Double-click the Try to Click Me! Button on the Form to display the button1_Click event method.
Embed the following lines (in bold only) to the method:
Private Sub Button1_Click(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]
[Link] = “Yes! I was Enabled!, thank you!”
End sub
9. Double-click the Enable Button! On the Form to display the radiobutton1_CheckedChanged
event method. Embed the following lines (in bold only) to the method:
Private Sub RadioButton1_CheckedChanged(ByVal sender As [Link], ByVal e
As [Link]) Handles RadioButton1_CheckedChanged
[Link] = True
[Link] = “You Enabled the Button!”
End sub
10. Double-click the Disabled Button! Radio button on the Form to display the
radioButton2_CheckedChange event method. Embed the following lines (in bold only) to the
method:
Private Sub RadioButton2_CheckedChanged(ByVal sender As [Link], ByVal e
As [Link]) Handles RadioButton1_CheckedChanged
[Link] = False
[Link] = “You Disabled the Button!”
End sub
11. Build and execute the application system by clicking the Start Debugging icon (or press F5) at the
Toolbar which color is a green arrow. Then, click the OK button.
Sample Output:
Figure 2.8a Disabling and Enabling Controls output
Explanation:
We can see here in the example above how easy it was to enable or disable a control. We just simply set
a particular control’s Enables property to true (for enabling) and false (for disabling) it. Take for example,
when the user clicks the Enable radio button (radioButton1), we enables it through the following code:
Private Sub RadioButton1_CheckedChanged(ByVal sender As [Link], ByVal e
As [Link]) Handles RadioButton1_CheckedChanged
[Link] = True
[Link] = “You Enabled the Button!”
End sub
Yes buddy, as simple as this code. It is very easy isn’t it? Okay, let us now go to how to hide or show a
control in the succedding example.
Example 9:
Design and develop an application system that shows or hides radio button 1. Follow the given figure
below in designing and developing the application system.
vbShowHideControl1
Buttons
Figure 2.9 Showing and Hiding Controls
Solution:
1. Start Visual Basic 2008 by clicking Start, All Programs, and Microsoft Visual Basic 2008 at the
Windows Vista operating system.
2. Create a new Windows Forms Application project by using either the file menu or the New
Project icon in the toolbar
3. Name the new application system vbShowHideControl1.
4. Set the Form’s Text property to vbShowHideControl1, too.
5. Click, drag and drop four (4) radio buttons from the Toolbox to add them into the Form.
6. Click, drag and drop two (2) Buttons from the Toolbox to add them into the Form, then set their
respective Text property to Hide Radio1!, and Show Radio1!.
7. Double-click the Hide radio1! Button on the Form to display the button1_Click event method.
Embed the following lines (in bold only) to the method:
Private Sub Button1_Click(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]
[Link] = False
End sub
8. Double-click the Show Radio1! Button on the Form to display the button1_Click event method.
Embed the following lines (in bold only) to the method:
Private Sub Button1_Click(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]
[Link] = true
End sub
9. Build and execute the application system by clicking the Start Debugging icon (or press F5) at the
Toolbar which color is a green arrow. Then, click the OK button.
Sample Output:
Figure 2.9a Showing and Hiding Controls output
Explanation:
We can see here in the example above how easy it was to hide or show a control. We just simply set a
particular control’s Visible property to true (for showing) and false (for hiding) it. Take for example,
when the user clicks the Hide radio button (radioButton1), we hide it through the following code:
Private Sub Button1_Click(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]
[Link] = False
End sub
Now, when the user clicks the Show radio button (radioButton2), we showed it through the following
code:
Private Sub Button1_Click(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]
[Link] = True
End sub
You could notice that the preceding codes are similar to the enabling and disabling a particular control
which was demonstrated in our previous example.
Tweaking the High School Level Example a little bit
Now the question is, what if we want to design an empty list of radio buttons? This is in the case of our
High School Level example when we saw that during the time we run our program, the default selection
will always be the First Year radio button, thus its corresponding displayed message is “Freshman!”.
What we want is to be able to display all the list of radio buttons as empty (unselected), sot that there is
no message displayed at the Text box, by default. Well, this is possible; however, we will apply a trick on
our program. Like any tricks, it works sometimes, and in most cases, it won’t. To work around this
programming task dilemma, we have to hide the first radio buttons as empty (or unselected). Very
clever, isn’t it. But no, don’t practice tricks, I warned you. It is but just a temporary success, and
eventually a permanent failure. Remember that our programs will unexpectedly produce side effects,
and these side-effects could be disastrous to our application system in the long run. Some technical
people call this one “quick fix”.
For the sake of showing you that we can turn around this programming task using tricks, let us do it
now. But again, this is just for demonstration purposes only.
Example 10:
Design and develop a simple Text box and