|
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
Chapter 28 Advanced Form, Control, And Windows Registry Handling
- If you need an immediate solution to:
- Passing Controls To Procedures
- Passing Control Arrays To Procedures
- Determining The Active Control
- Determining Control Type At Runtime
- Creating/Loading New Controls At Runtime
- Changing Control Tab Order
- Changing Control Stacking Position With Z-Order
- Drag/Drop: Dragging Controls
- Drag/Drop: Dropping Controls
- Handling Self-Drops When Dragging And Dropping
- Drag/Drop: Handling DragOver Events
- OLE Drag/Drop: Dragging Data
- OLE Drag/Drop: Dropping Data
- OLE Drag/Drop: Reporting The Drag/Drop Outcome
- Using The Lightweight Controls
- Passing Forms To Procedures
- Determining The Active Form
- Using The Form Objects Controls Collection
- Using the Forms Collection
- Setting A Forms Startup Position
- Keeping A Forms Icon Out Of The Windows 95 Taskbar
- Handling Keystrokes In A Form Before Controls Read Them
- Making A Form Immovable
- Showing Modal Forms
- Saving Values In The Windows Registry
- Getting Values From The Windows Registry
- Getting All Registry Settings
- Deleting A Registry Setting
In Depth
This is our chapter on advanced form and control techniquesand working with the Windows Registry. Well see many new techniques here, including dragging and dropping controls (and even how to drag and drop the data in controls, using OLE drag and drop). Programmers are often surprised when an advanced technique doesnt work in the same way that a basic technique does in Visual Basic. For example, a programmer might want to pass a control array to a procedureand immediately run into problems because that process doesnt work the same way as passing a normal array to a procedure. Heres how you pass a normal array to a procedure; first, you set up the array this way where we set up an integer array named intArray:
Private Sub Form_Load()
Dim intArray(4) As Integer
intArray(0) = 0
intArray(1) = 1
intArray(2) = 2
intArray(3) = 3
intArray(4) = 4
...
Then you pass the array to a procedure named, say, ShowValues, by specifying the name of the array with empty parentheses:
Private Sub Form_Load()
Dim intArray(4) As Integer
intArray(0) = 0
intArray(1) = 1
intArray(2) = 2
intArray(3) = 3
intArray(4) = 4
ShowValues intArray()
End Sub
In the procedure, you can work with the passed array as a normal array like this:
Private Sub ShowValues(TargetArray() As Integer)
For Each intValue In TargetArray
MsgBox intValue
Next
End Sub
TIP: You can even pass variable-length arrays of parameters to procedures using the ParamArray keyword.
Extrapolating the preceding technique to control arrays, you might think that youd pass a control array to a procedure in the same way. For example, if you had an array of command buttons named CommandArray, you might think you can pass it to a procedure, SetCaption, like this:
Private Sub Form_Load()
SetCaption CommandArray()
End Sub
In the SetCaption procedure, youd want to work with the control array as with a normal array, as in this case where we change the caption of each button to Button:
Private Sub SetCaption(TargetControl() As Control)
For Each ButtonObject In TargetControl
ButtonObject.Caption = "Button"
Next
End Sub
However, it doesnt work like thatin many ways, control arrays are really treated in Visual Basic like objects, not normal arrays. What that means here is that you have to pass the control array as an object (without the parentheses), like this:
Private Sub Form_Load()
SetCaption CommandArray
End Sub
Then in the procedure, you declare the control array as an Object, not as an array of type Control. However, you can treat it as an array like this:
Private Sub SetCaption(TargetControl As Object)
For Each ButtonObject In TargetControl
ButtonObject.Caption = "Button"
Next
End Sub
This is the kind of technique well take a look at in this chapterprogramming topics that might cause trouble unless you know the ins and outs of the situation. In addition, well take a look at two other broad programming areas: dragging and dropping controls, and working with the Windows Registry.
Drag And Drop And OLE Drag And Drop
One of the techniques well take a look at is dragging and dropping controls. Weve seen this technique in a few places in the book already, but well make our examination of this topic systematic in this chapter. Well see how to let the user drag and drop controls using the mouse. Users have only to press the mouse button over a control to start the process, and they can drag and drop controls in our applications as they like. This ability allows them to customize those applications.
Besides dragging and dropping controls in an application, its recently become popular to be able to drag data between applications. Well see how to do that here with OLE drag operations, which will let us drag data between the controls in our application, or to another application entirely; for example, well see how to let the user drag text directly from a text box in our program to another text boxor to a word processor like Microsoft Word.
The Windows Registry
Another popular aspect of Windows programming these days is the Windows Registry, a built-in part of Windows that provides long-term storage for program settings. For example, you can save the file names the user has most recently opened in a Most Recently Used (MRU) list at the bottom of the File menu. You can also store other settings for the program, such as window size and location, and use them when the program starts again.
Thats it for the overview of whats in this chapternow its time to turn to the Immediate Solutions section for advanced form, control, and Windows Registry handling.
Immediate Solutions
Passing Controls To Procedures
One easy way to handle a large number of controls is to set up a procedure that handles the controls in a generic way and pass the controls to that procedure. How do you pass a control to a procedure? You just declare it as type Control in the argument list of the procedure youre passing it to.
|