0% found this document useful (0 votes)
54 views

VB 2008 (Example 8)

The document describes examples of how to enable/disable and show/hide controls in a Visual Basic application. It provides the code to: 1) Enable/disable a button and display feedback in a text box when radio buttons are clicked. 2) Show/hide a radio button when buttons are clicked by setting the Visible property to True/False. 3) Discusses using tricks like hiding the first radio button to have no default selection, but warns against tricks as they can cause problems later.

Uploaded by

Guillan Pascual
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

VB 2008 (Example 8)

The document describes examples of how to enable/disable and show/hide controls in a Visual Basic application. It provides the code to: 1) Enable/disable a button and display feedback in a text box when radio buttons are clicked. 2) Show/hide a radio button when buttons are clicked by setting the Visible property to True/False. 3) Discusses using tricks like hiding the first radio button to have no default selection, but warns against tricks as they can cause problems later.

Uploaded by

Guillan Pascual
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

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 System.Object, ByVal e As


System.EventArgs) Handles Button1.click
TextBox1.Text = “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 System.Object, ByVal e
As System.EventArgs) Handles RadioButton1_CheckedChanged
Button1.Enabled = True
TextBox1.Text = “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 System.Object, ByVal e


As System.EventArgs) Handles RadioButton1_CheckedChanged
Button1.Enabled = False
TextBox1.Text = “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 System.Object, ByVal e


As System.EventArgs) Handles RadioButton1_CheckedChanged
Button1.Enabled = True
TextBox1.Text = “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 System.Object, ByVal e As


System.EventArgs) Handles Button1.click
RadioButton.Visible = 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 System.Object, ByVal e As


System.EventArgs) Handles Button2.click
RadioButton.Visible = 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 System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
RadioButton1.Visible = 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 System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
RadioButton1.Visible = 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

You might also like