Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Beginning Visual Basic 2005 (2006)

.pdf
Скачиваний:
226
Добавлен:
17.08.2013
Размер:
14.97 Mб
Скачать

Chapter 8

Private Sub undoToolStripMenuItem_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles undoToolStripMenuItem.Click

‘Declare a TextBox object and set it to the ActiveControl Dim objTextBox As TextBox = Me.ActiveControl

‘Undo the last operation objTextBox.Undo()

End Sub

2.The next menu item that you want to add code for is the Cut menu item. Select cutToolStripMenuItem in the Class Name combo and the Click event in the Method Name combo box. Add the highlighted code here:

Private Sub cutToolStripMenuItem_Click(ByVal sender As Object, _

ByVal e As System.EventArgs) Handles cutToolStripMenuItem.Click

‘Declare a TextBox object and set it to the ActiveControl Dim objTextBox As TextBox = Me.ActiveControl

‘Copy the text to the clipboard and clear the field objTextBox.Cut()

End Sub

3.You’ll want the Cut button on the toolbar to call the code for the Cut menu item. Select cutToolStripButton in the Class Name combo and the Click event in the Method Name combo box. Add the following highlighted code:

Private Sub cutToolStripButton_Click(ByVal sender As Object, _

ByVal e As System.EventArgs) Handles cutToolStripButton.Click

‘Call the cutToolStripMenuItem_Click procedure

cutToolStripMenuItem_Click(sender, e) End Sub

4.The next menu item that you need to code is the Copy menu item. Select copyToolStripMenuItem in the Class Name combo and the Click event in the Method Name combo box and then add the following highlighted code:

Private Sub copyToolStripMenuItem_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles copyToolStripMenuItem.Click

‘Declare a TextBox object and set it to the ActiveControl Dim objTextBox As TextBox = Me.ActiveControl

‘Copy the text to the clipboard objTextBox.Copy()

End Sub

5.You want the Copy button on the toolbar to call the procedure you just added. Select copyToolStripButton in the Class Name combo and the Click event in the Method Name combo box and then add the following highlighted code:

Private Sub copyToolStripButton_Click(ByVal sender As Object, _

ByVal e As System.EventArgs) Handles copyToolStripButton.Click

256

Creating Menus

‘Call the copyToolStripMenuItem_Click procedure

copyToolStripMenuItem_Click(sender, e) End Sub

6.The Paste menu item is next so select pasteToolStripMenuItem in the Class Name combo box and the Click event in the Method Name combo box. Add the following highlighted code to the Click event handler:

Private Sub pasteToolStripMenuItem_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles pasteToolStripMenuItem.Click

‘Declare a TextBox object and set it to the ActiveControl Dim objTextBox As TextBox = Me.ActiveControl

‘Copy the data from the clipboard to the textbox objTextBox.Paste()

End Sub

7.The Paste toolbar button should execute the code in the pasteToolStripMenuItem_Click procedure. Select pasteToolStripButton in the Class Name combo box and the Click event in the Method Name combo box and add the following highlighted code:

Private Sub pasteToolStripButton_Click(ByVal sender As Object, _

ByVal e As System.EventArgs) Handles pasteToolStripButton.Click

‘Call the pasteToolStripMenuItem_Click procedure

pasteToolStripMenuItem_Click(sender, e) End Sub

8.The last menu item under the Edit menu that you’ll write code for is the Select All menu item. Select selectAllToolStripMenuItem in the Class Name combo box and the Click event in the Method Name combo box and add the following highlighted code:

Private Sub selectAllToolStripMenuItem_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles selectAllToolStripMenuItem.Click

‘Declare a TextBox object and set it to the ActiveControl Dim objTextBox As TextBox = Me.ActiveControl

‘Select all text objTextBox.SelectAll()

End Sub

How It Works

You added the code for the Edit menu starting with the Undo menu item. Since you have two text boxes on your form, you need a way to determine which text box you are dealing with or a generic way of handling an undo operation for both text boxes. In this example, you go with the latter option and provide a generic way to handle both text boxes.

You do this by declaring a variable as a TextBox control and setting it to the ActiveControl property of the form, which retrieves the active control on the form. This is the control that has focus:

‘Declare a TextBox object and set it to the ActiveControl Dim objTextBox As TextBox = Me.ActiveControl

257

Chapter 8

Note that the menu and toolbar are never set as the active control. This allows you to use the menus and toolbar buttons and always reference the active control.

Now that you have a reference to the active control on the form, you can invoke the Undo method as shown in the last line of code here. The Undo method is a method of the TextBox class and will undo the last operation in that text box if it can be undone.

‘Undo the last operation

objTextBox.Undo()

The ActiveControl property works fine in this small example, because all you are dealing with is two text boxes. However, in a real-world application, you would need to test the active control to see whether it supports the method that you were using (for example, Undo).

The next menu item under the Edit menu that you write code for is the Cut menu item. The first thing that you do in this procedure is to again get a reference to the active control on the form and set it to the TextBox object that you declared.

Then you invoke the Cut method. This is a method of the TextBox class and will copy the selected text to the Clipboard and then remove the selected text from the text box:

‘Declare a TextBox object and set it to the ActiveControl Dim objTextBox As TextBox = Me.ActiveControl

‘Copy the text to the clipboard and clear the field objTextBox.Cut()

Since you have a Cut button on the toolbar, you want that procedure to call the Cut menu item procedure, because you have already written the code for that:

Private Sub cutToolStripButton_Click(ByVal sender As Object, _

ByVal e As System.EventArgs) Handles cutToolStripButton.Click

‘Call the cutToolStripMenuItem_Click procedure cutToolStripMenuItem_Click(sender, e)

End Sub

Next, you write code for the Copy menu item. Again, you declare a TextBox object and get a reference to the active control on the form for the Copy menu item. Then you invoke the Copy method to have the text in the active text box copied to the Clipboard:

‘Declare a TextBox object and set it to the ActiveControl Dim objTextBox As TextBox = Me.ActiveControl

‘Copy the text to the clipboard objTextBox.Copy()

You also have a Copy button on the toolbar and you want to call the procedure for the Copy menu item:

Private Sub copyToolStripButton_Click(ByVal sender As Object, _

ByVal e As System.EventArgs) Handles copyToolStripButton.Click

‘Call the copyToolStripMenuItem_Click procedure copyToolStripMenuItem_Click(sender, e)

End Sub

258

Creating Menus

For the Click event for the Paste menu item, the first thing that you do is to get a reference to the active control by setting it to your TextBox object that you have declared. Then you invoke the Paste method, which will paste the text contents of the Clipboard into your text box:

‘Declare a TextBox object and set it to the ActiveControl Dim objTextBox As TextBox = Me.ActiveControl

‘Copy the data from the clipboard to the textbox objTextBox.Paste()

Again you have the corresponding button on the toolbar, and you want the Paste button to call the procedure for the Paste menu item:

Private Sub pasteToolStripButton_Click(ByVal sender As Object, _

ByVal e As System.EventArgs) Handles pasteToolStripButton.Click

‘Call the pasteToolStripMenuItem_Click procedure pasteToolStripMenuItem_Click(sender, e)

End Sub

Lastly, for the Select All menu item, you once again get a reference to the active control by setting it to the TextBox object that you declared. Then you invoke the SelectAll method, which will select all text in the text box that is active:

‘Declare a TextBox object and set it to the ActiveControl Dim objTextBox As TextBox = Me.ActiveControl

‘Select all text objTextBox.SelectAll()

Coding the View Menu and Toolbars

Now that you added the code to make the Edit menu items and the corresponding toolbar buttons functional, the next step is to make the menu items under the View menu functional.

Try It Out

Coding the View Menu

1.In the Class Name combo box, select mainToolStripMenuItem and in the Method Name combo box select the Click event. Add the following highlighted code to the Click event handler:

Private Sub mainToolStripMenuItem_Click(ByVal sender As Object, _

ByVal e As System.EventArgs) Handles mainToolStripMenuItem.Click

‘Toggle the visibility of the Main toolbar ‘based on this menu item’s Checked property If mainToolStripMenuItem.Checked Then

tspMain.Visible = True

Else

tspMain.Visible = False End If

End Sub

259

Chapter 8

2.You need to add the same type of code that you just added to the Formatting submenu item. Select formattingToolStripMenuItem in the Class Name combo box and the Click event in the Method Name combo box and add the following highlighted code:

Private Sub formattingToolStripMenuItem_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles formattingToolStripMenuItem.Click

‘Toggle the visibility of the Formatting toolbar ‘based on this menu item’s Checked property

If formattingToolStripMenuItem.Checked Then tspFormatting.Visible = True

Else

tspFormatting.Visible = False End If

End Sub

How It Works

When the Main submenu item under the Tools menu item is clicked, the submenu item either displays a check mark or removes it based on the current state of the Checked property of the submenu item. You add code in the Click event of this submenu item to either hide or show the Main toolbar by setting its

Visible property to True or False:

‘Toggle the visibility of the Main toolbar ‘based on this menu item’s Checked property If mainToolStripMenuItem.Checked Then

tspMain.Visible = True

Else

tspMain.Visible = False End If

The same principle works for the Formatting submenu item, and its code is very similar to that of the Main submenu item:

‘Toggle the visibility of the Formatting toolbar ‘based on this menu item’s Checked property

If formattingToolStripMenuItem.Checked Then tspFormatting.Visible = True

Else

tspFormatting.Visible = False End If

Testing Your Code

As your applications become more complex, testing your code becomes increasingly important. The more errors that you find and fix during your testing, the better able you will be to implement an application that is both stable and reliable for your users. This translates into satisfied users and earns a good reputation for you for delivering a quality product.

You need not only to test the functionality of your application but also to test various scenarios that a user might encounter or perform. For example, suppose you have a database application that gathers user input from a form and inserts it into a database. A good application will validate all user input before trying to

260

Creating Menus

insert the data into the database, and a good test plan will try to break the data validation code. This will ensure that your validation code handles all possible scenarios and functions properly.

Try It Out

Testing Your Code

1.It’s now time to test your code! Click the Run toolbar button. When your form loads, the only toolbar that you should see is the main toolbar, as shown in Figure 8-7.

Figure 8-7

2.Click the View menu and then click the Toolbars menu item. Notice that the Main submenu item is checked and the main toolbar is visible. Go ahead and click the Formatting submenu item. The Formatting toolbar is displayed along with the main toolbar.

Notice that the controls on your form shifted down when the Formatting toolbar was displayed. The reason that this happened was that you placed a Panel control on your form, set its Dock property to Fill, and then placed your TextBox controls on the Panel control. Doing this allows the controls on your form to be repositioned, either to take up the space when a toolbar is hidden or to make room for the toolbar when it is shown; much like the behavior in Microsoft Word or Visual Studio 2005.

3.If you click the View menu again and then click the Toolbars menu item, you will see that both the Main and Formatting submenu items are checked. The checked submenu items indicate that the toolbar is visible, and an unchecked submenu item indicates that the toolbar is not visible.

4.Now test the functionality of the Edit menu. Click in the first text box and type some text. Then click the Edit menu and select the Select All menu item. Once you select the Select All menu item, the text in the text box is highlighted.

5.You now want to copy the text in the first text box while the text is highlighted. Hover your mouse over the Copy button on the toolbar to view the ToolTip. Now either click on the Copy button on the toolbar or select the Edit Copy menu item.

Place your cursor in the second text box, and then either click the Paste button on the toolbar or select Edit Paste. The text is pasted into the second text box, as shown in Figure 8-8.

6.Click the first text box and then click Edit Undo. Notice that the changes you made to the first text box have been undone. You might have expected that the text in the second text box would be removed, but Windows keeps track of the cut, copy, and paste operations for each control individually; so there’s nothing you need to do.

7.The last item on the Edit menu to test is the Cut menu item. Type some more text in the first text box, and then highlight the text in the first text box by clicking the Edit menu and selecting the Select All menu item. Then either click the Cut icon on the toolbar or select Edit Cut. The text is copied to the Clipboard and is then removed from the text box.

261

Chapter 8

Figure 8-8

Place your cursor in the second text box at the end of the text there. Then paste the text in this text box using the Paste shortcut key Ctrl+V. The text has been placed at the end of the existing text in the text box. This is how Windows’ cut, copy, and paste operations work, and, as you can see, there was very little code required to implement this functionality in your program.

8.Now click the File menu and choose the New menu item. The text in the text boxes is cleared. The only menu item left to test is the Exit menu item under the File menu.

9.Before testing the Exit menu item, take a quick look at context menus. Type some text in one of the text boxes. Now, right-click in that text box, and you will see a context menu pop up as shown in Figure 8-9. Notice that this context menu appeared automatically; there was no code that you needed to add to have this done. This is a feature of the Windows operating system, and Visual Studio 2005 provides a way to override the default context menus, as you will see in the next section.

Figure 8-9

10.To test the last bit of functionality of your program, select File Exit, and your program will end.

Context Menus

Context menus are menus that pop up when a user clicks the right mouse button over a control or window. This provides the users with quick access to the most commonly used commands for the control that they are working with. As you just saw, the context menu that appeared provides you with a way to manage the text in a text box.

262

Creating Menus

Context menus are customized for the control that you are working with, and in more complex applications, such as Visual Studio 2005 or Microsoft Word, they provide quick access to the commands for the task that is being performed.

You saw that Windows provides a default context menu for the TextBox control that you are working with, and you can override the default context menu if your application’s needs dictate that you do so. For example, suppose that you have an application in which you want the user to be able to copy the text in a text box but not actually cut or paste text in that text box. This would be an ideal situation to provide your own context menu to allow only the operations that you want.

Visual Studio 2005 provides a ContextMenuStrip control that you can place on your form and customize, just as you did the MenuStrip control. However, the main difference between the MenuStrip control and the ContextMenuStrip control is that you can create only one top-level menu with the ContextMenuStrip control. You can still create submenu items with the ContextMenuStrip if you need to.

Most controls in the toolbox have a ContextMenuStrip property that can be set to the context menu that you define. When you right-click that control, the context menu that you defined is displayed instead of the default context menu.

Some controls, such as the ComboBox and ListBox controls, do not have a default context menu. This is because they contain a collection of items, not a single item like simple controls such as the TextBox. They do, however, have a ContextMenuStrip property that can be set to a context menu that you define.

The ComboBox control does not provide a context menu when its DropDownStyle property is set to Drop DownList, but it does provide a context menu when its DropDownStyle property is set to Simple or

DropDown.

Creating Context Menus

Now that you know what context menus are, you are ready to learn how to create and use them in your Visual Basic 2005 applications. In the next Try It Out, you will be expanding the code in the previous Try It Out section by adding a context menu to work with your text boxes. You add one context menu and use it for both text boxes. You could just as easily create two context menus, one for each text box, and have the context menus perform different functions.

Try It Out

Creating Context Menus

1.View the form in the Forms Designer and then click the toolbox to locate the ContextMenuStrip control. Drag and drop it onto your form. It will be added at the bottom of the development environment just as the MenuStrip control was.

2.In the Properties window, click the ellipsis dots button next to the Items property. You’ll be adding five menu items in your context menu in the next several steps.

3.Click the Add button in the Items Collection Editor dialog box to add the first menu item and set the Name property to contextUndoToolStripMenuItem. Click the ellipsis dots button next to the Image property and then click the Import button in the Select Resource dialog box. Locate an Undo bitmap or icon file on your computer and click the Open button. Click OK in the Select Resource dialog box to close it and to return to the Items Collection Editor. Locate the Text property and set it to Undo.

263

Chapter 8

4.You want to add a separator between the Undo menu item and the next menu item. Select Separator in the List combo box in the Items Collection Editor dialog box and then click the Add button. You’ll want to accept all default properties for the separator.

5.Now select MenuItem in the combo box and click the Add button again to add the next menu item and set the Name property to contextCutToolStripMenuItem. Click the ellipsis dots button next to the Image property and, in the Select Resource dialog box, locate a Cut bitmap or icon file. Finally, set the Text property to Cut.

6.Click the Add button again to add the next menu item and set the Name property to context CopyToolStripMenuItem. Click the ellipsis dots button next to the Image property and, in the Select Resource dialog box, locate a Copy bitmap or icon file. Finally, set the Text property to

Copy.

7.Click the Add button once again to add the next menu item and set the Name property to contextPasteToolStripMenuItem. Click the ellipse button next to the Image property and in the Select Resource dialog box, import the PASTE.BMP file. Then set the Text property to Paste.

8.Now you want to add a separator between the Paste menu item and the next menu item. Select Separator in the combo box in the Items Collection Editor dialog box and then click the Add button. Again, you’ll want to accept all default properties for the separator.

9.Now select MenuItem in the combo box and click the Add button to add the final menu item. Set the Name property to contextSelectAllToolStripMenuItem and the Text property to Select All. There is no image for this menu item. Finally, click OK in the Items Collection Editor dialog box to close it.

10.When you are done, click any part of the form, and the context menu will disappear. (You can always make it reappear by clicking the ContextMenuStrip1 control at the bottom of the development environment.)

11.Click the first text box on your form. In the Properties window, select ContextMenuStrip1 in the drop-down list for the ContextMenuStrip property. Repeat the same action for the second text box to assign a context menu in the ContextMenuStrip property.

12.Now test your context menu for look and feel. At this point, you haven’t added any code to it, but you can ensure that it looks visually correct. Run the application; then right-click in the first text box, and you will see the context menu that you have just added, as shown in Figure 8-10. The same context menu will appear if you also right-click in the second text box.

13.Stop your program and switch to the Code Editor for your form so that you can add the code for the context menus. The first procedure that you want to add is that for the Undo context menu item. Select contextUndoToolStripMenuItem in the Class Name combo box and the Click event in the Method Name combo box and add the following highlighted code:

Private Sub contextUndoToolStripMenuItem_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles contextUndoToolStripMenuItem.Click

‘Call the undoToolStripMenuItem_Click procedure

undoToolStripMenuItem_Click(sender, e) End Sub

264

Creating Menus

Figure 8-10

14.Select contextCutToolStripMenuItem in the Class Name combo box and the Click event in the Method Name combo box. Add the following highlighted code to the Click event handler:

Private Sub contextCutToolStripMenuItem_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles contextCutToolStripMenuItem.Click

‘Call the cutToolStripMenuItem_Click procedure

cutToolStripMenuItem_Click(sender, e) End Sub

15.For the Copy context menu item, select contextCopyToolStripMenuItem in the Class Name combo box and the Click event in the Method Name combo box and then add the following highlighted code:

Private Sub contextCopyToolStripMenuItem_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles contextCopyToolStripMenuItem.Click

‘Call the copyToolStripMenuItem_Click procedure

copyToolStripMenuItem_Click(sender, e) End Sub

16.Select contextPasteToolStripMenuItem in the Class Name combo box for the Paste context menu item and the Click event in the Method Name combo box. Then add the following highlighted code:

Private Sub contextPasteToolStripMenuItem_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles contextPasteToolStripMenuItem.Click

‘Call the pasteToolStripMenuItem_Click procedure

pasteToolStripMenuItem_Click(sender, e) End Sub

17.The last procedure that you need to perform is for the Select All context menu item. Select contextSelectAllToolStripMenuItem in the Class Name combo box and the Click event in the Method Name combo box and then add the following highlighted code:

Private Sub contextSelectAllToolStripMenuItem_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) _

Handles contextSelectAllToolStripMenuItem.Click

265