Click Here!
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


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 Object’s Controls Collection
Using the Forms Collection
Setting A Form’s Startup Position
Keeping A Form’s 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 techniques—and working with the Windows Registry. We’ll 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 doesn’t 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 procedure—and immediately run into problems because that process doesn’t work the same way as passing a normal array to a procedure. Here’s 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 you’d 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, you’d 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 doesn’t work like that—in 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 we’ll take a look at in this chapter—programming topics that might cause trouble unless you know the ins and outs of the situation. In addition, we’ll 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 we’ll take a look at is dragging and dropping controls. We’ve seen this technique in a few places in the book already, but we’ll make our examination of this topic systematic in this chapter. We’ll 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, it’s recently become popular to be able to drag data between applications. We’ll 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, we’ll see how to let the user drag text directly from a text box in our program to another text box—or 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.

That’s it for the overview of what’s in this chapter—now it’s 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 you’re passing it to.


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.