Writing a Procedure to Control Option Buttons

1. Activate the InfoSurvey form, and double-click the Software option button located in the Main Interest frame.

2. When the code window appears with the optSoft_Click procedure skeleton, highlight the code and press Delete.

3. Click the down arrow in the upper right-hand side combo box, and select the Change event procedure. Visual Basic will automatically enter the beginning and end of the optSoft_Change procedure for you.

4. Enter the code of the optSoft_Change procedure, as shown below:

Private Sub optSoft_Change() Me.lboxSystems.Clear Call ListSoftware Me.lboxSystems.Listlndex = 0 Me.picImage.Picture = LoadPicture("C:\Books.bmp") End Sub

The optSoft_Change procedure begins with the statement that uses the Clear method to remove the current list of items from the lboxSystems list box. The next statement calls the ListSoftware procedure to populate the list box with software items. In other words, when the user clicks the Software button, the procedure removes the hardware items from the list box and adds the software items. If you don't clear the list box prior to adding new items, the new items will be appended to the current list. The statement Me.lboxSystems.Listlndex = 0 selects the first item in the list. The final statement in this procedure loads a picture file to the image control. Be sure to replace the reference to this file with the complete path to a valid picture file that is located in your computer.

Because the user may want to reselect the Hardware button after selecting the Software button, you must create a similar Change event procedure for the optHard option button.

5. Enter the following optHard_Change procedure, just below the optSoft_Change procedure:

Private Sub optHard_Change() Me.lboxSystems.Clear Call ListHardware Me.lboxSystems.Listlndex = 0 Me.picImage.Picture = LoadPicture("C:\cd.bmp") End Sub

6. Launch the form from the Survey button in the Info Survey worksheet and check the results. When you click the Software option button, you should see the software items in the list box below. At the same time, the image control should display the assigned picture. After clicking the Hardware option button, the list box should display the appropriate hardware items. At the same time, the image control should display a different picture.

7. Close the form by clicking the Close button in the form's upper right-hand corner.

0 0

Post a comment