244 247




Visual Basic 6 Black Book:Command Buttons, Checkboxes, And Option Buttons
function GetCookie (name) { var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; while (i < clen) { var j = i + alen; if (document.cookie.substring(i, j) == arg) { var end = document.cookie.indexOf (";", j); if (end == -1) end = document.cookie.length; return unescape(document.cookie.substring(j, end)); } i = document.cookie.indexOf(" ", i) + 1; if (i == 0) break; } return null; } var m1=''; var gifstr=GetCookie("UsrType"); if((gifstr!=0 ) && (gifstr!=null)) { m2=gifstr; } document.write(m1+m2+m3);            Keyword Title Author ISBN Publisher Imprint Brief Full  Advanced      Search  Search Tips Please Select ----------- Components Content Mgt Certification Databases Enterprise Mgt Fun/Games Groupware Hardware IBM Redbooks Intranet Dev Middleware Multimedia Networks OS Prod Apps Programming Security UI Web Services Webmaster Y2K ----------- New Titles ----------- Free Archive 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 function isIE4() { return( navigator.appName.indexOf("Microsoft") != -1 && (navigator.appVersion.charAt(0)=='4') ); } function bookMarkit() { var url="http://www.itknowledge.com/PSUser/EWBookMarks.html?url="+window.location+"&isbn=0"; parent.location.href=url; //var win = window.open(url,"myitk"); //if(!isIE4()) // win.focus(); } Search this book:  














Previous
Table of Contents
Next





Handling Button Releases
You can tell when a button’s been pushed using its Click event, but can you tell when it’s been released? Yes, using the MouseUp event. In fact, buttons support the MouseDown, MouseMove, MouseUp, KeyDown, KeyPress, and KeyUp events.
To determine when a button’s been released, you can just use its MouseUp event this way:


Private Sub Command1_MouseUp(Button As Integer, Shift As Integer,_
X As Single, Y As Single)
MsgBox "You released the button."
End Sub


This can be useful if you want the user to complete some action that has two parts; for example, you can use MouseDown to begin changing (for example, incrementing or decrementing) a setting of some kind in realtime, giving the user interactive visual feedback, and you can use MouseUp to freeze the setting when the user releases the button.
Making A Command Button Into A Cancel Button
When you’re designing dialog boxes, you usually include an OK button and a Cancel button. In fact, you can skip the OK button if you have other ways of letting the user select options (for example, a Finish button or a Yes button), but a Cancel button is just about required in dialog boxes. You should always have a Cancel button to let the user close the dialog box in case he has opened it by mistake or changed his mind.

Command buttons do have a Cancel property, and Microsoft recommends that you set it to True if you are making a command button into a Cancel button. Only one button can be a Cancel button in a form.
However, there doesn’t seem to be much utility in making a command button into a Cancel button. There’s nothing special about that button, really—it won’t automatically close a dialog box, for example—except for one thing: when the user hits the Esc key, the Cancel button is automatically clicked. Using the Esc key is one way users have of closing dialog boxes, but it’s not a very compelling reason to have a separate Cancel property for buttons.
Tellingly, the Cancel button in the predefined dialog box that comes with Visual Basic (you can add it when you select Project|Add Form) does not have its Cancel property set to True.
Getting A Checkbox’s State
You’ve added all the checkboxes you need to your new program, WinBigSuperCasino, and you’ve connected those checkboxes to Click event handlers. But now there’s a problem—when the users set the current amount of money they want to bet, you need to check if they’ve exceeded the limit they’ve set for themselves. But they set their limit by clicking another checkbox. How can you determine which one they’ve checked?
You can see if a checkbox is checked by examining its Value property (Visual Basic does have a Checked property, but that’s only for menu items, a fact that has confused more than one programmer). Here are the possible Value settings for checkboxes:

•  0— Unchecked
•  1— Checked
•  2— Grayed

Here’s an example; in this case, we will change a command button’s caption if a checkbox, Check1, is checked, but not otherwise:


Private Sub Command1_Click()
If Check1.Value = 1 Then
Command1.Caption = "The check mark is checked"
End If
End Sub


Setting A Checkbox’s State
Your new program, SuperSandwichesToGoRightNow, is just about ready, but there’s one hitch. You use checkboxes to indicate what items are in a sandwich (cheese, lettuce, tomato, and more) to let users custom-build their sandwiches, but you also have a number of specialty sandwiches with preset ingredients. When the user selects one of those already-built sandwiches, how do you set the ingredients checkboxes to show what’s in them?
You can set a checkbox’s state by setting its Value property to one of the following:

•  0—Unchecked
•  1—Checked
•  2—Grayed

Here’s an example; In this case, we check a checkbox, Check1, from code:


Private Sub Command1_Click()
Check1.Value = 1
End Sub


Here’s another example that uses the Visual Basic Choose() function to toggle a checkbox’s state each time the user clicks the command button Command1:


Private Sub Command1_Click()
Check1.Value = Choose(Check1.Value + 1, 1, 0)
End Sub


Grouping Option Buttons Together
When you add option buttons to a form, they are automatically coordinated so that only one option button can be selected at a time. If the user selects a new option button, all the other options buttons are automatically deselected. But there are times when that’s not convenient. For example, you may have two sets of options buttons: days of the week and day of the month. You want the user to be able to select one option button in each list. How do you group option buttons together into different groups on the same form?

You can use the frame control to group option buttons together (and, in fact, you can also use Picture Box controls). Just draw a frame for each group of option buttons you want on a form and add the option buttons to the frames (in the usual way—just select the Option Button tool and draw the option buttons in the frames). Each frame of option buttons will act as its own group, and the user can select one option button in either group, as shown in Figure 7.14.

Figure 7.14  Grouping option buttons together using frames.
For organizational purposes, and if appropriate, you might consider making the option buttons in each group into a control array, which can make handling multiple controls easier.

Getting An Option Button’s State
You can check if an option button is selected or not with the Value property. Unlike checkboxes, which have three settings for the Value property (corresponding to checked, not checked, and grayed), option buttons’ Value property only has two settings: True if the button is selected, and False if not.
Here’s an example showing how to see whether or not an option button is selected. In this case, we display a message in a message box that indicates if an option button, Option1, is selected:


Private Sub Command1_Click()
If Option1.Value Then
MsgBox "The option button is selected."
Else
MsgBox "The option button is not selected."
End If
End Sub


And that’s all there is to it.




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.



Wyszukiwarka

Podobne podstrony:
244 245
247 07
247 a
237 244
247 249
Whirlpool FL 244
242 244

więcej podobnych podstron