|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Lets see an example. Here, well pass a command button, Command1, to a procedure, SetCaption, to set that buttons caption to Button when the form containing the button first loads:
Private Sub Form_Load()
SetCaption Command1
End Sub
In the SetCaption procedure, you declare the passed button as type Control:
Private Sub SetCaption(TargetControl As Control)
TargetControl.Caption = "Button"
End Sub
And thats itnow were passing controls to procedures. Passing Control Arrays To ProceduresIts reasonable to think that control arrays are really arrays, but in fact, you sometimes have to treat them as objects, not arrays (which is why you can create event handlers for control arrayssomething you certainly couldnt do if they were just Visual Basic arrays). For example, when you want to pass a control array to a procedure, you pass it as an Object, not an array of type Control. Lets see an example. Here, we pass a control array of buttons, CommandArray, to a procedure, SetCaption (note that you omit the empty parentheses after CommandArray, which you would include if that array were a normal array and not a control array):
Private Sub Form_Load()
SetCaption CommandArray
End Sub
Now in SetCaption, we declare the passed array as type Object: Private Sub SetCaption(TargetControl As Object) End Sub Then we can use the passed control array as we would any control array; here we set the caption of each command button in the array to Button:
Private Sub SetCaption(TargetControl As Object)
For Each ButtonObject In TargetControl
ButtonObject.Caption = "Button"
Next
End Sub
Thats itnow we can pass control arrays to procedures. Determining The Active ControlThe Testing Department is on the phone again. How about letting the user customize your program by clicking a control and then selecting a menu item to, say, set the controls background color? Well, you say, how do I know which control the user has clicked before using the menu item? Use the ActiveControl property, they say. The ActiveControl property of forms indicates which control on the form is currently active. Using this property, you can determine which control currently has the focus.
Lets see an example. In this case, when the user clicks the form, well set the active controls Caption property to Active Control:
Private Sub Form_Click()
ActiveControl.Caption = "Active Control"
End Sub
The result of this code appears in Figure 28.1, where we indicate the active control when the user clicks the form.
Theres a problem with this code, howeverthe form might contain all kinds of controls, some of which (like text boxes) dont even have a Caption property. If the user clicks the form when one of those controls is the active control, an error will result. To fix this potential problem, we can check the type of the active control before working with it, and well see how to do that in the next topic. Determining Control Type At RuntimeIn the previous topic, we set the caption of the currently active control (that is, the control with the focus) to Active Control when the user clicked the form, but realized there was a problem if the active control was one that didnt have a Caption property. To determine what type of control youre working with at runtime, you can use TypeOf keyword, which you can use to determine the type of any object. At first, it seems odd that you might not know what kind of control youre working with, but cases like the current onewhere were using the control now stored in the forms ActiveControl propertyare very common in Visual Basic programming. In the previous topic, we set the caption of the active control when the user clicked the form like this:
Private Sub Form_Click()
ActiveControl.Caption = "Active Control"
End Sub
Now we can add code to check the active controls type (possible types for Visual Basic controls include CommandButton, CheckBox, ListBox, OptionButton, HScrollBar, VScrollBar, ComboBox, Frame, PictureBox, Label, TextBox, and so on) this way, making sure the active control is a command button before changing its caption:
Private Sub Form_Click()
If TypeOf ActiveControl Is CommandButton Then
ActiveControl.Caption = "Active Control"
End If
End Sub
Creating/Loading New Controls At RuntimeThe Testing Department is on the phone again. Your program, SuperDuperDataCrunch, just doesnt have enough buttons to please some users. You ask, hows that again? Lets add some way to let the user create new buttons at runtime, they say. To load new controls at runtime, you must have a control array. This makes a lot of sense, actually, because you can set up event handlers for the controls in a control array, and a new control just represents a new index in such an event handler. If you didnt have a control array, youd need to set up an event handler for the new control that named the new control by namebefore it existedwhich the Visual Basic compiler couldnt do. When you have a control array, you just use the Load statement: Load object In this case, object is the new control in the control array. Lets see an example. Here, well place four buttons in a form and add a fifth when the user clicks the form. When the user does click the form, we should add a new button, and we start that process by calculating the index for the new control in the control array. That new controls index, which well call intNextIndex, is the index after the current end of the control array, and we determine that with the Ubound property:
Private Sub Form_Click()
Dim intNextIndex As Integer
intNextIndex = CommandArray.UBound + 1
...
Then we use the Load statement to create this new control:
Private Sub Form_Click()
Dim intNextIndex As Integer
intNextIndex = CommandArray.UBound + 1
Load CommandArray(intNextIndex)
...
|
|
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. |