|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Lets see an example. Here, well add code to our oledrag example that weve developed in the previous few examples to report what happened when users drop the data theyve dragged from Text1 into Text2 using Text1s 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 books accompanying CD-ROM.
Using The Lightweight ControlsThe Testing Department is on the phone again. Your program, SuperDuperDataCrunch, sure is using up a lot of memory. Cant 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 dont 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 Components dialog box.
You can add the lightweight controls to a program, as shown in Figure 28.7, where you see the complete set of lightweight controls.
From the Visual Basic programmers 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 ProceduresCan you pass forms to procedures in Visual Basic? You certainly canjust declare them with the keywords As Form in the argument list. Lets 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 were 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 FormYouve 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 objects 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, well 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 timers 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 Form1s 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.
The code for this example is located in the twoclocks folder on this books 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 Objects Controls CollectionIf you want to work with all the controls in a form indexed in an array, use the forms Controls collection. You can loop over all controls in a form using this collection. Lets 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
Thats 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.
Using the Forms CollectionIn the previous topic, we saw that you can loop over all the controls in a form using the forms Controls collection. You can also loop over all the forms in an application using the Visual Basic Global objects Forms collection. Lets see an example. Here, well 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
|
|
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. |