|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Form Drawing MethodsBesides 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, well take a look at those methods as well. Thats it for the overviewits time to turn to the Immediate Solutions. Immediate SolutionsAdding A Frame To A ProgramThe 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 frames 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, weve given the left frame in Figure 14.4 the caption Day of the week and the tool tip Enter the day here.
Setting Frame Size And LocationSetting a frames height and width is easyjust set the frames Height and Width properties at design time or runtime. You can set the frames location in its container (that is, a form or picture box) with its Left and Top properties, or by using its Move method.
For example, heres how you use a frames 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 ControlsThe Aesthetic Design Department is on the phone. The way youve set up your controls in your program is finebut what if users want to move them around at runtime? Shouldnt 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 well take a look at how to drag controls now. To start a drag operation, you use the controls Drag method: Control.Drag action Here, the action parameter can take these values:
Lets 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, well 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, well need to know that original mouse location before we move the controls upper left corner. Well 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 controls 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
Theres one more thing to consider hereusers 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 were 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
Thats itwhen 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.
The code for this example is located in the dragdrop folder on this books accompanying CD-ROM.
|
|
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. |