230 235




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




Setting Button Text Color
You’ve got your graphic design program working at last. But wouldn’t it be a nice touch if you could set the captions in the color-selection buttons to match the colors the buttons correspond to? For example, the button with the red text lets the user select red as the drawing color, the button with the green text lets the user select green, and so on. You can set the color of a button’s caption using the button’s ForeColor property.
Interestingly, only checkboxes and option buttons have a ForeColor property; command buttons do not.
You set a button’s ForeColor property at design time, as in Figure 7.3, or at runtime like this:


Private Sub Check1_Click()
Check1.ForeColor = RGB(255, 0, 0)
End Sub



Figure 7.3  Setting a button’s ForeColor property at design time.

Setting Button Fonts
You’ve written an adventure-type game for your grandfather, but he’s emailed to let you know he can’t read the “tiny text” in the buttons. He likes to run his screen in super high-resolution mode. Can you fix that?

Yes you can. All you have to do is to make the font size in the buttons’ captions larger. To do that, you use the button’s Font property. Selecting the Font item in the Properties window opens the Font dialog box shown in Figure 7.4. As you can see in that figure, captions can go up to 24 point, which should be big enough for grandfather.
Notice that there are number of options in the Font dialog box in Figure 7.4, which means that you can’t set a single property at runtime to set a button’s font. Instead, you can use the following properties:

•  FontBold
•  FontItalic
•  FontName
•  FontSize
•  FontStrikethru
•  FontUnderline


Figure 7.4  Selecting a font for a button.
You also have direct access to the button’s Font object, so you can set those properties by referring to them as, for example, Option1.Font.Bold, Option1.Font.Italic, and so on.
Reacting To Button Clicks
For completeness, we’ll include this one here: You respond to button clicks with the button’s Click event. To add a Click event handler, just double-click the button at design time, which adds a subroutine like this one:


Private Sub Command1_Click()

End Sub


Place the code you want to execute when the button is clicked in this subroutine:



Private Sub Command1_Click()
MsgBox "You clicked the command button!"
End Sub


All three buttons have a Click event—they wouldn’t be much use otherwise—and option buttons also have a double-click event, DblClick. If you double-click a checkbox, you select and then deselect it (or deselect and then select it), so you’re back to where you started. If you double-click an option button, however, you select it, no matter what its original state, and cause a DblClick event.
Creating Button Control Arrays
You’ve decided that your new game program really does need 144 buttons in the main form, arranged in a grid of 12×12. But what a pain it is to write 144 sub-routines to handle the click event for each of them! Isn’t there a better way?

There is. You use a control array and one event handler function (the control array index of the button that was clicked is passed to the event handler, so you can tell which button you need to respond to). To create a control array, just give two controls of the same type the same name (in the Name property); when you do, Visual Basic will ask if you want to create a control array, as in Figure 7.5.

Figure 7.5  Creating a control array.
When you create an event handler subroutine for a button in the control array, Visual Basic will automatically pass the index of the control in the control array to that subroutine:



Private Sub GamePiece_Click(Index As Integer)

End Sub


You can then refer to the control that caused the event as a member of an array, using the index passed to the subroutine:



Private Sub GamePiece_Click(Index As Integer)
GamePiece(Index).Caption = "You clicked me!"
End Sub



TIP:  When you add controls to a control array, the first one has Index 0, the next has Index 1, and so on. You can change the index of each control with its Index property, rearranging the controls in the control array as you like.

You can also create a control array with just one control—just set that control’s Index property to 0. Later, you can add more controls to the array at runtime if you like, using the Load statement (see “Adding Buttons At Runtime” later in this chapter).
Resetting The Focus After A Button Click
When you click a button, the input focus is transferred to the button—and in some cases, you don’t want that to happen. For example, say you’ve got a word-processor program based on a rich text box control, and you have a button labeled “Search” in the program. When the user clicks the button, then we can search for target text in the rich text box using that box’s UpTo() method—but the focus remains on the button the user clicked. When the user starts typing again, nothing appears in the rich text box control because the focus is still on the button. How do you transfer the focus back to the rich text box?
You do that with the control’s SetFocus() method, which is something you frequently do in real programs after button clicks. Here’s how it might look in code:


Private Sub Command1_Click()
RichTextBox1.UpTo (gstrStringToFind)
RichTextBox1.SetFocus
End Sub


Now, when the user clicks the button and starts typing again, the focus will be back on the rich text box, as it should be. Note that you can set the control that has the focus when a form first appears by setting the control’s Default property to True (only one control on a form may have that property set to True).

TIP:  Buttons also have two events—GotFocus and LostFocus—that can tell you when your button has gotten or lost the focus.





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:
25 (230)
0 611 235 708 GBH 7 DE
20 (235)
225 230
05 (230)
Arctic48 3 235
230,23,artykul
235 kent

więcej podobnych podstron