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


Form Drawing Methods

Besides using the preceding controls to draw lines and circles in forms, you can actually use methods built into the form to do much the same thing. Because this is a chapter about designing and organizing your controls on forms, we’ll take a look at those methods as well.

That’s it for the overview—it’s time to turn to the Immediate Solutions.

Immediate Solutions

Adding A Frame To A Program

The Testing Department is calling again. Do you really need 200 buttons in your program? Of course, you say. They say, well, can you please organize them into groups? Hmm, you think, how do you do that?

You can use frames to group controls together in forms or picture boxes. To draw a frame, you just use the Frame Control tool in the toolbox as you would for any control. When you add a frame to a form or picture box, there are a few things you should know. To set the text that appears at upper left in a frame, you set the frame’s Caption property (and you can change the caption at runtime by setting this property). You can make frames appear flat or 3D (the default) by setting their Appearance property. You can also give frames tool tips (the explanatory text that appears in a small yellow window when the mouse cursor rests over a control) by setting the ToolTipText property.

For example, we’ve given the left frame in Figure 14.4 the caption “Day of the week” and the tool tip “Enter the day here”.


Figure 14.4  Organizing controls with frames.


TIP:  To group option buttons together in a frame, see “Grouping Controls In A Frame” later in this chapter.

Setting Frame Size And Location

Setting a frame’s height and width is easy—just set the frame’s Height and Width properties at design time or runtime. You can set the frame’s location in its container (that is, a form or picture box) with its Left and Top properties, or by using its Move method.


TIP:  The frame contains other controls, such as option buttons, they move with the frame.

For example, here’s how you use a frame’s Move method:

Private Sub Command1_Click()
    Frame1.Move Frame1.Left + 1000
End Sub

For more on dragging frames, take a look at the next topic.

Dragging And Dropping Controls

The Aesthetic Design Department is on the phone. The way you’ve set up your controls in your program is fine—but what if users want to move them around at runtime? Shouldn’t they be able to do that?

You drag and drop frames just as you do any other control, and dragging frames also drags all the controls in that frame, so we’ll take a look at how to drag controls now.

To start a drag operation, you use the control’s Drag method:

Control.Drag action

Here, the action parameter can take these values:

  vbCancel—0; cancels drag operation
  vbBeginDrag—1; begins dragging object
  vbEndDrag—2; ends dragging and drops object

Let’s see this at work. For example, when the user drags a frame, Frame1, in our program, we catch the MouseDown event first:

Private Sub Frame1_MouseDown(Button As Integer, Shift As Integer, X As _
    Single, Y As Single)

End Sub

When the control is dropped, we’ll get the new mouse location at the upper left of the control. However, because the mouse was originally pressed at some location inside the control, and not at its upper left corner, we’ll need to know that original mouse location before we move the control’s upper left corner. We’ll save that mouse location inside the control as (intXoffset, intYoffset):

Private Sub Frame1_MouseDown(Button As Integer, Shift As Integer, X As _
    Single, Y As Single)
    intXOffset = X
    intYOffset = Y
...
End Sub

Declare these new variables as form-wide variables in the (General) section of the form:

Dim intXOffset As Integer
Dim intYOffset As Integer

Then we start the drag operation of the control itself with the Drag method:

Private Sub Frame1_MouseDown(Button As Integer, Shift As Integer, X As _
    Single, Y As Single)
    intXOffset = X
    intYOffset = Y
    Frame1.Drag 1
End Sub

When the control is dropped on the form, we just move the upper left of the control to the mouse location minus the offset of the mouse in the control (this avoids making the control’s upper left corner appear to jump to the mouse location). Doing so looks like this in code:

Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single)
    Source.Move X - intXOffset, Y - intYOffset
End Sub

There’s one more thing to consider here—users may just move the frame a little distance, in which case they are actually dropping the control on top of itself. In this case, the new mouse position we’re passed is relative to the upper left of the control, so we have to add the Left and Top values to the mouse location to get form coordinates:

Private Sub Frame1_DragDrop(Source As Control, X As Single, Y As Single)
    Source.Move Source.Left + X - intXOffset, Source.Top + Y - intYOffset
End Sub

That’s it—when you run the program, you can drag Frame1, the left frame in Figure 14.5, as you like. Our drag and drop example is a success.


Figure 14.5  Dragging a frame control with all the controls in it.

The code for this example is located in the dragdrop folder on this book’s accompanying CD-ROM.


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.