|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Controls are originally loaded as invisible (in case you want to work with it off screen first), so we make our new button visible by setting its Visible property to True:
Private Sub Form_Click()
Dim intNextIndex As Integer
intNextIndex = CommandArray.UBound + 1
Load CommandArray(intNextIndex)
CommandArray(intNextIndex).Visible = True
...
Now we can treat this new button as any other button; here, we set its caption to New button and place it in the center of the form this way:
Private Sub Form_Click()
Dim intNextIndex As Integer
intNextIndex = CommandArray.UBound + 1
Load CommandArray(intNextIndex)
CommandArray(intNextIndex).Visible = True
CommandArray(intNextIndex).Caption = "New button"
CommandArray(intNextIndex).Move ScaleWidth / 2 - _
CommandArray(intNextIndex).Width / 2, ScaleHeight / 2 - _
CommandArray(intNextIndex).Height / 2
End Sub
In addition, we can handle events from the new button in the event handler for the whole button array, because the index of the control that caused the event is passed to us in that event handler:
Private Sub CommandArray_Click(Index As Integer)
MsgBox "You clicked button " & Index
End Sub
The result of this code appears in Figure 28.2, where weve added a new button by just clicking the form. The code for this example is located in the loadcontrols folder on this books accompanying CD-ROM.
Changing Control Tab OrderThe Testing Department is calling again. About the keyboard interface youve set up for your program, SuperDuperDataCrunchcant you let the user customize the tab order for the controls? Sure, you say, whats tab order? They explain, thats the order in which the focus moves from control to control when the user presses the Tab button. Each control that can accept the focus has a TabIndex property, and when the user presses the Tab key, the focus moves from control to control, following the tab order as set by the controls TabIndex properties. (The first control in the tab order has TabIndex = 0.) You can change the tab order at runtime by changing the value in your controls TabIndex properties. Lets see an example. Here, we add three buttons to a form, Command1, Command2, and Command3, which, by default, have the TabIndex properties 0, 1, and 2, respectively. To change that tab order when the user clicks the form, we can use buttons TabIndex properties like this, where we reverse the tab order:
Private Sub Form_Click()
Command1.TabIndex = 2
Command2.TabIndex = 1
Command3.TabIndex = 0
End Sub
Changing Control Stacking Position With Z-OrderThe Aesthetic Design Department is on the phone. It takes an awfully long time to load and change pictures of the companys founders in that large picture box you have in your program: isnt there a better way? There is. Instead of loading the images into a picture box when needed, you can place a number of picture boxes on top of each other and display them one at a time by setting the picture boxes Z-order. Z-order is the stacking order for controls, and you can set controls Z-orders with the ZOrder method: Control.ZOrder position The position argument is an integer that indicates the position of the control relative to other controls of the same type. If position is 0 or omitted, Control is placed at the front of the Z-order, on top of the other controls. If position is 1, Control is placed at the back of the Z-order. Lets see an example. Here, we place two picture boxes, Picture1 and Picture2, in a form, with Picture2 on top of Picture1. When the user clicks the form, we can move Picture1 to the top with the ZOrder method:
Private Sub Form_Click()
Picture1.ZOrder 0
End Sub
Drag/Drop: Dragging ControlsThe Aesthetic Design Department is on the phone again. There are still some customization issues with your program, SuperDuperDataCrunch. Cant you let the users drag all the controls and place them where they want? Hmm, you say, how does that work? To enable a control for drag operations, make sure its DragMode property is set to Manual (= 0, the default), not Automatic (= 1); when DragMode is manual, we can handle drag operations from code. When the user presses the mouse in a control, you can start a drag operation with the Drag method: Control.Drag action Here, the action argument can take these values:
The user can then drag the control to a new position on the form and release it, causing a DragDrop event in the form, and you can move the control in that event. Here, the control thats been dropped is passed in the Source argument, and the position of the mouse is passed as (X, Y): Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single) End Sub Lets see an example. In this case, well add six text boxes to a form in a control array named Textboxes, and when the form first loads, we display the text Drag me in each text box:
Private Sub Form_Load()
For Each objText In Textboxes
objText.Text = "Drag me"
Next
End Sub
We also add a MouseDown event handler to the text box control array so we can start the dragging operation when the user presses the mouse button in the control:
Private Sub Textboxes_MouseDown(Index As Integer, Button As Integer, Shift _
As Integer, X As Single, Y As Single)
End Sub
When the user drops the control, well be given the location of the mouse in the form, but to position the control correctly, we also need the original position of the mouse in the control. That is, if the user pressed the mouse button in the middle of the control, we need to move the middle of the control (not the controls origin, the upper-left corner) to the new mouse location. Therefore, we need to save the mouses original location in the control when the user presses the mouse button. Well save the mouses original location in two integers, intXOffset and intYOffset, making these form-wide variables: Dim intXOffset, intYOffset As Integer
|
|
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. |