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 add code to our oledrag example that we’ve developed in the previous few examples to report what happened when users drop the data they’ve dragged from Text1 into Text2 using Text1’s OLECompleteDrag event:

Private Sub Text1_OLECompleteDrag(Effect As Long)
    MsgBox "Returned OLE effect: " & Effect
End Sub

Now the program displays the OLE effect value when the user drops the OLE data, as shown in Figure 28.6. The code for this example is located in the oledrag folder on this book’s accompanying CD-ROM.


Figure 28.6  Reporting the results of an OLE drag/drop operation.

Using The Lightweight Controls

The Testing Department is on the phone again. Your program, SuperDuperDataCrunch, sure is using up a lot of memory. Can’t you do something about it? You ask, any suggestions? They say, how about using lightweight controls to replace the 200 command buttons in the program?

To save memory, you can use the Microsoft lightweight controls, also called the windowless controls because they don’t include all the internal machinery needed to support a window and window procedure. The lightweight controls come in the ActiveX control group named MSWLess.ocx.

You add them to a project with the Project[vbar]Components menu item, clicking the Controls tab and selecting the Microsoft Windowless Controls entry in the Component’s dialog box.


TIP:  If that entry does not appear in the Components dialog box, you must register MSWLess.ocx with Windows using the utility regsvr32.exe that comes with Windows and Visual Basic.

You can add the lightweight controls to a program, as shown in Figure 28.7, where you see the complete set of lightweight controls.


Figure 28.7  The windowless lightweight controls.

From the Visual Basic programmer’s point of view, there are really only two differences between the standard Visual Basic controls and the lightweight controls: the lightweight controls do not have a hWnd property, and they do not support Dynamic Data Exchange (DDE). Besides those two differences, using a lightweight control is just like using a standard control; for example, you can add items to the WLlist1 list box this way when the form loads:

Private Sub Form_Load()
    WLList1.AddItem "WLlist1"
End Sub

Or you can have the computer beep using the Visual Basic Beep statement when the user clicks the command button WLCommand1:

Private Sub WLCommand1_Click()
    Beep
End Sub

Passing Forms To Procedures

Can you pass forms to procedures in Visual Basic? You certainly can—just declare them with the keywords As Form in the argument list.

Let’s see an example. Here, we set up a subroutine named SetColor that will set the background color of forms you pass to that subroutine, and we pass the current form to SetColor when that form loads:

Private Sub Form_Load()
    SetColor Me
End Sub

In the SetColor subroutine, we declare the passed form this way, giving it the name TargetForm:

Public Sub SetColor(TargetForm As Form)

End Sub

Now we’re free to use the passed form as we would any form:

Public Sub SetColor(TargetForm As Form)
    TargetForm.BackColor = RGB(0, 0, 255)
End Sub

Determining The Active Form

You’ve got a multiform program and need to work with the controls on the currently active form (that is, the form with the focus)—but how do you determine which form is the active form?

You can use the Visual Basic Screen object’s ActiveForm property to determine which form is active. For example, say we had a clock program with two forms, Form1 and Form2, each with a label control, Label1, in which we can display the current time using a timer, Timer1. However, we’ll only update the time in the active form, which the user can switch simply by clicking the forms with the mouse.

To display the time, we add the timer, Timer1, to Form1, and set its Interval property to 1000 (as measured in milliseconds). Now we can use the Label1 control in the currently active form in the timer’s Timer event this way:

Private Sub Timer1_Timer()
    Screen.ActiveForm.Label1.Caption = Format(Now, "hh:mm:ss")
End Sub

We also make sure the second form, Form2, is shown when the program loads with Form1’s Load event:

Private Sub Form_Load()
    Form2.Show
End Sub

You can see the result of this code in Figure 28.8. When you click one of the two forms, that form displays and updates the time, until you click the other form, which makes that other form take over.


Figure 28.8  Determining the active form.

The code for this example is located in the twoclocks folder on this book’s accompanying CD-ROM. (This is the form with the name Form1 in our example; Form2 in this program is just a standard form with a label control, Label1, in it.)

Using The Form Object’s Controls Collection

If you want to work with all the controls in a form indexed in an array, use the form’s Controls collection. You can loop over all controls in a form using this collection. Let’s see an example. Here, we fill a form with command buttons and then set the caption of each control to “Button” when the user clicks the form:

Private Sub Form_Click()
    For Each ButtonControl In Form1.Controls
        ButtonControl.Caption = "Button"
    Next
End Sub

That’s all the code we need. Now the user can click the form to set the captions of all the buttons at once, as shown in Figure 28.9.


Figure 28.9  Using the Controls collection to set captions

Using the Forms Collection

In the previous topic, we saw that you can loop over all the controls in a form using the form’s Controls collection. You can also loop over all the forms in an application using the Visual Basic Global object’s Forms collection.

Let’s see an example. Here, we’ll display three forms, Form1, Form2, and Form3, displaying Form2 and Form3 when Form1 loads:

Private Sub Form_Load()
    Form2.Show
    Form3.Show
End Sub

Now when the user clicks a button in Form1 named, say, CloseAll, we can hide all open forms this way using the Forms collection:

Private Sub CloseAll_Click()
    For Each Form In Forms
        Form.Hide
    Next Form
End Sub


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.