1. Trang chủ
  2. » Công Nghệ Thông Tin

Visual Basic 6 Black Book phần 9 pdf

112 369 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 112
Dung lượng 3,36 MB

Nội dung

ActiveControl.Caption = "Active Control" End Sub Now we can add code to check the active control’s type (possible types for Visual Basic controls include CommandButton, CheckBox, ListBox, OptionButton, HScrollBar, VScrollBar, ComboBox, Frame, PictureBox, Label, TextBox, and so on) this way, making sure the active control is a command button before changing its caption: Private Sub Form_Click() If TypeOf ActiveControl Is CommandButton Then ActiveControl.Caption = "Active Control" End If End Sub Creating/Loading New Controls At Runtime The Testing Department is on the phone again. Your program, SuperDuperDataCrunch, just doesn’t have enough buttons to please some users. You ask, how’s that again? Let’s add some way to let the user create new buttons at runtime, they say. To load new controls at runtime, you must have a control array. This makes a lot of sense, actually, because you can set up event handlers for the controls in a control array, and a new control just represents a new index in such an event handler. If you didn’t have a control array, you’d need to set up an event handler for the new control that named the new control by name—before it existed—which the Visual Basic compiler couldn’t do. When you have a control array, you just use the Load statement: Load object In this case, object is the new control in the control array. Let’s see an example. Here, we’ll place four buttons in a form and add a fifth when the user clicks the form. When the user does click the form, we should add a new button, and we start that process by calculating the index for the new control in the control array. That new control’s index, which we’ll call intNextIndex, is the index after the current end of the control array, and we determine that with the Ubound property: Private Sub Form_Click() Dim intNextIndex As Integer intNextIndex = CommandArray.UBound + 1 Then we use the Load statement to create this new control: Private Sub Form_Click() Dim intNextIndex As Integer Visual Basic 6 Black Book:Advanced Form, Control, And Windows Registry Handling http://24.19.55.56:8080/temp/ch28\0970-0974.html (3 of 4) [3/14/2001 2:08:53 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com intNextIndex = CommandArray.UBound + 1 Load CommandArray(intNextIndex) Visual Basic 6 Black Book:Advanced Form, Control, And Windows Registry Handling http://24.19.55.56:8080/temp/ch28\0970-0974.html (4 of 4) [3/14/2001 2:08:53 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 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 we’ve added a new button by just clicking the form. The code for this example is located in the loadcontrols folder on this book’s accompanying CD-ROM. Figure 28.2 Adding a new control to a form at runtime. Changing Control Tab Order The Testing Department is calling again. About the keyboard interface you’ve set up for your program, SuperDuperDataCrunch—can’t you let the user customize the tab order for the controls? Sure, you say, what’s tab order? They explain, that’s 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 control’s 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 Visual Basic 6 Black Book:Advanced Form, Control, And Windows Registry Handling http://24.19.55.56:8080/temp/ch28\0974-0977.html (1 of 3) [3/14/2001 2:09:08 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com your controls’ TabIndex properties. Let’s 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-Order The Aesthetic Design Department is on the phone. It takes an awfully long time to load and change pictures of the company’s founders in that large picture box you have in your program: isn’t 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. Let’s 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 Controls The Aesthetic Design Department is on the phone again. There are still some customization issues with your program, SuperDuperDataCrunch. Can’t 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: • vbCancel—0; cancels the drag operation. • vbBeginDrag—1; begins dragging the control. • vbEndDrag—2; ends the drag operation. Visual Basic 6 Black Book:Advanced Form, Control, And Windows Registry Handling http://24.19.55.56:8080/temp/ch28\0974-0977.html (2 of 3) [3/14/2001 2:09:08 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 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 that’s 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 Let’s see an example. In this case, we’ll 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, we’ll 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 control’s origin, the upper-left corner) to the new mouse location. Therefore, we need to save the mouse’s original location in the control when the user presses the mouse button. We’ll save the mouse’s original location in two integers, intXOffset and intYOffset, making these form-wide variables: Dim intXOffset, intYOffset As Integer Visual Basic 6 Black Book:Advanced Form, Control, And Windows Registry Handling http://24.19.55.56:8080/temp/ch28\0974-0977.html (3 of 3) [3/14/2001 2:09:08 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Here’s how we save those integers and start the drag operation with the Drag method: Private Sub Textboxes_MouseDown(Index As Integer, Button As Integer, Shift _ As Integer, X As Single, Y As Single) intXOffset = X intYOffset = Y Textboxes(Index).Drag vbBeginDrag End Sub Now the user is dragging the control—and we’ll see how to let the user drop it in the next topic. Drag/Drop: Dropping Controls When the user drops a control on a form, you get a DragDrop event: Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single) End Sub Here, the control that’s been dropped is passed in the Source argument, and the position of the mouse is passed as (X, Y). You can use the control’s Move method to move the control to the new position. Let’s see an example. In the previous topic, we let users drag a text box in a form. When they drop it, we’ll get a DragDrop event in the form and can move the text box to the new location—after taking into account the original mouse position in the control with the x and y offsets, intXOffset and intYOffset: Dim intXOffset, intYOffset As Integer Private Sub Form_Load() For Each objText In Textboxes objText.Text = "Drag me" Next End Sub Private Sub Textboxes_MouseDown(Index As Integer, Button As Integer, Shift _ As Integer, X As Single, Y As Single) intXOffset = X intYOffset = Y Textboxes(Index).Drag vbBeginDrag End Sub Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single) Source.Move X - intXOffset, Y - intYOffset End Sub And that’s all it takes—now you can drag and drop controls in a form, as shown in Figure 28.3. Figure 28.3 Dragging and dropping controls in a form. Visual Basic 6 Black Book:Advanced Form, Control, And Windows Registry Handling http://24.19.55.56:8080/temp/ch28\0977-0982.html (1 of 4) [3/14/2001 2:09:13 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com However, there’s a problem here. When you move a text box to an entirely new location in the form, the drag/drop operation goes smoothly. However, if you drag the text box just a short distance, that text box jumps back to its original position when you release it—what’s going on? Here’s what’s happening: if you drag the text box just a short distance and drop it, Visual Basic thinks you’re dropping it on itself, and instead of triggering a form DragDrop event, it triggers a DragDrop event for the text box itself. To complete our drag/drop example, we’ll take care of this “self-drop” problem in the next topic. The code for this example, dragcontrols.frm version 1, appears in Listing 28.1. (Version 2, which is located in the dragcontrols folder on this book’s accompanying CD-ROM, will take care of the “self-drop” problem.) Listing 28.1 dragcontrols.frm version 1 VERSION 6.00 Begin VB.Form Form1 Caption = "Form1" ClientHeight = 3195 ClientLeft = 60 ClientTop = 345 ClientWidth = 4680 LinkTopic = "Form1" ScaleHeight = 3195 ScaleWidth = 4680 StartUpPosition = 3 'Windows Default Begin VB.TextBox Textboxes Height = 495 Index = 5 Left = 1920 TabIndex = 5 Top = 1920 Width = 1215 End Begin VB.TextBox Textboxes Height = 495 Index = 4 Left = 1920 TabIndex = 4 Top = 1200 Width = 1215 End Begin VB.TextBox Textboxes Height = 495 Index = 3 Left = 1920 TabIndex = 3 Top = 240 Width = 1215 End Begin VB.TextBox Textboxes Height = 495 Visual Basic 6 Black Book:Advanced Form, Control, And Windows Registry Handling http://24.19.55.56:8080/temp/ch28\0977-0982.html (2 of 4) [3/14/2001 2:09:13 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Index = 2 Left = 360 TabIndex = 2 Top = 1920 Width = 1215 End Begin VB.TextBox Textboxes Height = 495 Index = 1 Left = 360 TabIndex = 1 Top = 1080 Width = 1215 End Begin VB.TextBox Textboxes Height = 495 Index = 0 Left = 360 TabIndex = 0 Top = 240 Width = 1215 End End Attribute VB_Name = "Form1" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False Dim intXOffset, intYOffset As Integer Private Sub Form_Load() For Each objText In Textboxes objText.Text = "Drag me" Next End Sub Private Sub Textboxes_MouseDown(Index As Integer, Button As Integer,_ Shift As Integer, X As Single, Y As Single) intXOffset = X intYOffset = Y Textboxes(Index).Drag vbBeginDrag End Sub Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single) Source.Move X - intXOffset, Y - intYOffset End Sub Handling “Self-Drops” When Dragging And Dropping In the previous two topics, we handled drag/drop operations for controls in a form, but there was a problem. If the Visual Basic 6 Black Book:Advanced Form, Control, And Windows Registry Handling http://24.19.55.56:8080/temp/ch28\0977-0982.html (3 of 4) [3/14/2001 2:09:13 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com user doesn’t move a control very far, we get a DragDrop event for the control itself, because Visual Basic acts as though the user is dropping the control on itself, not on the form. We can handle this case by adding a DragDrop event handler to the control itself. Let’s see how this works in an example. We’ll add a DragDrop event handler for the text boxes in the previous example, the dragcontrols project: Dim intXOffset, intYOffset As Integer Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single) Source.Move X - intXOffset, Y - intYOffset End Sub Private Sub TextBoxes_DragDrop(Index As Integer, Source As Control, X As_ Single, Y As Single) End Sub Now when the user drags a control a little way and drops it on top of itself, we can move the control to its new position. Note that we are passed mouse coordinates local to the control in the DragDrop event and have to translate them to form-based coordinates to use the Move method: Dim intXOffset, intYOffset As Integer Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single) Source.Move X - intXOffset, Y - intYOffset End Sub Private Sub TextBoxes_DragDrop(Index As Integer, Source As Control, X As_ Single, Y As Single) Source.Move X + Textboxes(Index).Left - intXOffset, Y + _ Textboxes(Index).Top - intYOffset End Sub Visual Basic 6 Black Book:Advanced Form, Control, And Windows Registry Handling http://24.19.55.56:8080/temp/ch28\0977-0982.html (4 of 4) [3/14/2001 2:09:13 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com That’s all it takes—now users can move the text boxes in the dragcontrols example around as they like. The code for this example is located in the dragcontrols folder on this book’s accompanying CD-ROM. Drag/Drop: Handling DragOver Events When the user drags a control over a form or control, a DragOver event is triggered like this: Sub Form_DragOver(source As Control, x As Single, y As Single, state As_ Integer) Here are the arguments this event handler is passed: • source—The control being dragged. • x, y—Position of the mouse in the target form or control. These coordinates are set in terms of the target’s coordinate system (as set by the ScaleHeight, ScaleWidth, ScaleLeft, and ScaleTop properties). • state—The transition state of the control being dragged in relation to a target form or control: Enter = 0, source control is being dragged into the target; Leave = 1, source control is being dragged out of the target; Over = 2, source control has moved in the target. Let’s see an example. Here, we’ll turn the text boxes in the dragcontrols example that we’ve developed in the previous few topics blue as the user drags a control over them. To do that, we add a DragOver event to the text boxes in the Textboxes control array: Private Sub Textboxes_DragOver(Index As Integer, Source As Control, X As _ Single, Y As Single, State As Integer) End Sub Here, we simply add code to turn the text box blue when we drag another control over it: Private Sub Textboxes_DragOver(Index As Integer, Source As Control, X As _ Single, Y As Single, State As Integer) Textboxes(Index).BackColor = RGB(0, 0, 255) End Sub And that’s it—now we’re handling DragOver events, as shown in Figure 28.4. Figure 28.4 Handling DragOver events. OLE Drag/Drop: Dragging Data The Testing Department is on the phone again. A lot of new word processors are allowing users to drag data from application to application—how about your new SuperDuperTextPro program? It’s not possible, you say. Yes it is, they say, use OLE drag/drop. In OLE drag/drop operations, you can let the user drag data between controls, and even between programs. Here’s how it works: when the user presses the mouse button, you start the OLE drag operation with the Visual Basic 6 Black Book:Advanced Form, Control, And Windows Registry Handling http://24.19.55.56:8080/temp/ch28\0982-0986.html (1 of 3) [3/14/2001 2:09:18 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com [...]... File not found 54 55 57 58 59 Bad file mode File already open Device I/O error File already exists Bad record length http://24. 19. 55. 56: 8080/temp/ch 29\ 1001-1007.html (2 of 5) [3/14/2001 2: 09: 48 AM] Visual Basic 6 Black Book: Error Handling And Debugging 61 62 63 67 68 70 71 74 75 76 91 92 93 94 97 98 Disk full Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Input past end of file... this book s accompanying CD-ROM, will report back to Text1 what happened when Text2 accepted the data) Listing 28.2 oledrag.frm version 1 VERSION 6. 00 Begin VB.Form Form1 Caption = ClientHeight = ClientLeft = ClientTop = ClientWidth = LinkTopic = ScaleHeight = "Form1" 3 195 60 345 468 0 "Form1" 3 195 http://24. 19. 55. 56: 8080/temp/ch28\ 09 86- 099 0.html (2 of 4) [3/14/2001 2: 09: 20 AM] Visual Basic 6 Black Book: Advanced... Figure 28 .9 Figure 28 .9 Using the Controls collection to set captions Using the Forms Collection In the previous topic, we saw that you can loop over all the controls in a form using the form’s Controls collection You can also loop over all the forms in an application using the Visual Basic http://24. 19. 55. 56: 8080/temp/ch28\ 099 0- 099 5.html (3 of 4) [3/14/2001 2: 09: 35 AM] Visual Basic 6 Black Book: Advanced... Next Form End Sub http://24. 19. 55. 56: 8080/temp/ch28\ 099 0- 099 5.html (4 of 4) [3/14/2001 2: 09: 35 AM] Visual Basic 6 Black Book: Advanced Form, Control, And Windows Registry Handling Setting A Form’s Startup Position Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com You can set the position of a form when it is first displayed by positioning it in the Visual Basic IDE’s forms window,... form loads: Private Sub Form_Load() http://24. 19. 55. 56: 8080/temp/ch28\ 099 0- 099 5.html (1 of 4) [3/14/2001 2: 09: 35 AM] Visual Basic 6 Black Book: Advanced Form, Control, And Windows Registry Handling WLList1.AddItem "WLlist1" End Sub PDF Merge and Split Unregistered Version - http://www.simpopdf.com Simpo Or you can have the computer beep using the Visual Basic Beep statement when the user clicks the command... collection Variable uses a type not supported in Visual Basic http://24. 19. 55. 56: 8080/temp/ch 29\ 1001-1007.html (4 of 5) [3/14/2001 2: 09: 48 AM] Visual Basic 6 Black Book: Error Handling And Debugging 4 59 This component doesn’t support the set of events Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 460 Invalid Clipboard format 461 Specified format doesn’t match format of data... fact with the OLECompleteDrag event: http://24. 19. 55. 56: 8080/temp/ch28\ 09 86- 099 0.html (3 of 4) [3/14/2001 2: 09: 20 AM] Visual Basic 6 Black Book: Advanced Form, Control, And Windows Registry Handling Sub object_CompleteDrag([ effect As Long]) Simpo PDF Merge and Split Unregistered Here, the effect parameter can take these values: Version - http://www.simpopdf.com • vbDropEffectNone—0; drop target cannot... using data stored in the Registry http://24. 19. 55. 56: 8080/temp/ch28\ 099 5- 099 8.html (3 of 3) [3/14/2001 2: 09: 42 AM] Visual Basic 6 Black Book: Advanced Form, Control, And Windows Registry Handling We stored the MRU file name in a Registry section named “Settings” and gave that file name the key Simpo PDF program’s form first loads, Version - http://www.simpopdf.com “Doc1” When theMerge and Split Unregistered... control in the currently active form in the timer’s Timer event this way: http://24. 19. 55. 56: 8080/temp/ch28\ 099 0- 099 5.html (2 of 4) [3/14/2001 2: 09: 35 AM] Visual Basic 6 Black Book: Advanced Form, Control, And Windows Registry Handling Private Sub Timer1_Timer() Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Screen.ActiveForm.Label1.Caption = Format(Now, "hh:mm:ss") End Sub We also... from itself after the move.) http://24. 19. 55. 56: 8080/temp/ch28\ 09 86- 099 0.html (4 of 4) [3/14/2001 2: 09: 20 AM] Visual Basic 6 Black Book: Advanced Form, Control, And Windows Registry Handling Let’s see an example Here, we’ll add code to our oledrag example that we’ve developed in the Simpo examples to report what happened when - http://www.simpopdf.com previous few PDF Merge and Split Unregistered Version . using the Visual Basic Visual Basic 6 Black Book: Advanced Form, Control, And Windows Registry Handling http://24. 19. 55. 56: 8080/temp/ch28 099 0- 099 5.html (3 of 4) [3/14/2001 2: 09: 35 AM] Simpo PDF Merge. OLECompleteDrag event: Visual Basic 6 Black Book: Advanced Form, Control, And Windows Registry Handling http://24. 19. 55. 56: 8080/temp/ch28 09 86- 099 0.html (3 of 4) [3/14/2001 2: 09: 20 AM] Simpo PDF Merge and. the move.) Visual Basic 6 Black Book: Advanced Form, Control, And Windows Registry Handling http://24. 19. 55. 56: 8080/temp/ch28 09 86- 099 0.html (4 of 4) [3/14/2001 2: 09: 20 AM] Simpo PDF Merge and

Ngày đăng: 14/08/2014, 01:20

TỪ KHÓA LIÊN QUAN

w