About   -   Contact   -   Purchase   -   Search   -   What's New

Adding Shortcuts for Edit Menus
Sometimes it's just easier to use the quick and simple method. Here, the Edit Menu is a good case in point. If you need to Cut, Copy, or Paste the contents of a text box, or if you need to Undo the last thing done, it can be a lot simpler to just let Windows do all the work for you.

To try this routine start Visual Basic and create a New Project consisting of only one form. Create an Edit Menu for the form. The Edit Menu should look like the following:

Caption Name Index Shortcut
&Undo mnuEdit 0 CTRL+U
Cu&t mnuEdit 1 CTRL+T
& Copy mnuEdit 2 CTRL+C
&Paste mnuEdit 3 CTRL+P
Now, place the code behind the menu's click event procedure. Since the menu was set up as an array, only one event procedure is necessary. We can determine which menu item was selected by the Index argument passed to the procedure. The following routine sends the appropriate keystrokes to the form which Windows intercepts and handles accordingly. The code looks like the following:
Sub mnuEdit_Click (Index As Integer)
'<DESC>		Routine for handling common edit menu 
'		functions</DESC>
'<ARGS>		Index: Edit Menu Index<ARGS>
'<USAGE>	mnuEdit_Click(1)</USAGE>

    Select Case Index
    Case 0
	SendKeys "%{BACKSPACE}"     ' Undo
    Case 1
	SendKeys "+{DELETE}"        ' Cut
    Case 2
	SendKeys "^{INSERT}"        ' Copy
    Case 3
	SendKeys "+{INSERT}"        ' Paste
    End Select 

End Sub 

Now, place a couple of text boxes on the form. To test this procedure, run the program and enter some text into one of the text boxes. Select the text and choose Cut from the Edit Menu. Then select the second text box and choose Paste from the Edit Menu. Then select the first text box and choose Undo from the Edit Menu. Continue playing around with the menu to see how it all works.


Top of Page

Legal Notice

Ken Howe 2011