home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Setting An Option Button’s State

Besides examining an option button’s state, you can also set it using the Value property. The Value property can take two values: True or False.

Here’s an example. In this case, we just set an option button, Option1, to its selected state by setting its Value property to True:

Private Sub Command1_Click()
    Option1.Value = True
End Sub

And that’s all it takes.

Using Graphical Checkboxes And Radio Buttons

The Aesthetic Design Department is on the phone again. Your new program is fine, but it lacks a certain pizzazz. You say, Pizzazz? They say, how about using something better than option buttons? Something more graphical.

As it happens, Visual Basic can help out here, because it does support graphical—that is, image-oriented—buttons. You add an image to a button by connecting an image (as from an image file) to its Picture property. When you’re working with checkboxes and option buttons, you should also set the button’s DownPicture property to specify what image it should display when selected (in other words, when the button is “down”).

Graphical checkboxes and option buttons look like image-bearing command buttons, not standard checkboxes and option buttons. The only way you tell them apart from command buttons when the program runs is that checkboxes and option buttons, when clicked, stay clicked (and, of course, option buttons still function in groups).

To see how this works, we set the Picture and DownPicture properties of a set of option buttons to image files (using the Picture and DownPicture entries in the Properties window). We also must set the Style property of the option buttons to 1 to make them graphical buttons, and then run the program. As you can see in Figure 7.15, the option buttons now display images: one when the button is selected and another (as in the top button in Figure 7.15) when the button is selected.


Figure 7.15  Using graphical option buttons.

You can also add images to buttons in code using the Visual Basic LoadPicture() function. For example, here’s how we load in a new down picture for Option1 when the user clicks it:

Private Sub Option1_Click(Index As Integer)
    Option1.DownPicture = LoadPicture("c:\vbbb\picturebuttons\image.bmp")
End Sub

Using Checkboxes And Option Buttons Together

Your new assignment: to create a program for the SuperDuper Excelsior Tours travel agency. It wants to present users with a list of four tour packages they can choose from and to list the destination cities in each tour. But SuperDuper also wants to let users customize their tours to some extent, so they should be able to add or remove cities from a tour package.

Sounds like a job for Visual Basic. In fact, it sounds like a job for both option buttons and checkboxes, because this is just how they are intended to work together: the option buttons let you select one (and only one) option from a list, and the checkboxes display which item or items (that is, one or more than one) correspond to that option. And because checkboxes are interactive controls, users can use them to set the items they want.

To actually write the program the travel agency wants, we add two frames to a form, as shown in Figure 7.16, giving the first frame the caption “Tour” and the second frame the caption “Cities”. In addition, add the option buttons and checkboxes you see in Figure 7.16.


Figure 7.16  The tour packages program.

When the user clicks Package 1, corresponding to the first tour package, we can indicate what cities are in this tour by setting the appropriate checkboxes:

Private Sub Option1_Click()
    Check1.Value = 1
    Check2.Value = 0
    Check3.Value = 1
    Check4.Value = 0
End Sub

And that’s how the program works; we can do the same for the other option buttons now:

Private Sub Option2_Click()
    Check1.Value = 0
    Check2.Value = 1
    Check3.Value = 0
    Check4.Value = 1
End Sub

Private Sub Option3_Click()
    Check1.Value = 1
    Check2.Value = 1
    Check3.Value = 0
    Check4.Value = 0
End Sub

Private Sub Option4_Click()
    Check1.Value = 1
    Check2.Value = 1
    Check3.Value = 1
    Check4.Value = 1
End Sub

And that’s it—now run the program as shown in Figure 7.16. When you click one option button, the corresponding tour’s cities are displayed in the checkboxes; when you click another option button, that tour’s cities are displayed.

This program, then, offers a good example of how the unique capabilities of option buttons and checkboxes may be integrated into the same program. The complete code for the form in Figure 7.16, tourpackages.frm, is located in the tourpackages folder on this book’s accompaning CD-ROM.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.