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


Let’s see an example. Here, we’ll pass a command button, Command1, to a procedure, SetCaption, to set that button’s 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 that’s it—now we’re passing controls to procedures.

Passing Control Arrays To Procedures

It’s 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 arrays—something you certainly couldn’t 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.

Let’s 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

That’s it—now we can pass control arrays to procedures.

Determining The Active Control

The 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 control’s 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.


TIP:  Besides forms, the Screen object also has an ActiveControl property, so if you have a number of forms, you can determine the active control with the Screen object’s ActiveControl property.

Let’s see an example. In this case, when the user clicks the form, we’ll set the active control’s 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.


Figure 28.1  Indicating the active control in a form.

There’s a problem with this code, however—the form might contain all kinds of controls, some of which (like text boxes) don’t 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 we’ll see how to do that in the next topic.

Determining Control Type At Runtime

In 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 didn’t have a Caption property.

To determine what type of control you’re 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 you’re working with, but cases like the current one—where we’re using the control now stored in the form’s ActiveControl property—are 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 control’s 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 Runtime

The Testing Department is on the phone again. Your program, SuperDuperDataCrunch, just doesn’t have enough buttons to please some users. You ask, how’s that again? Let’s 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 didn’t have a control array, you’d need to set up an event handler for the new control that named the new control by name—before it existed—which the Visual Basic compiler couldn’t 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. Let’s see an example. Here, we’ll 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 control’s index, which we’ll 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)
...


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.