
Beginning Visual Basic 2005 (2006)
.pdf
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




