VB Tutorial 7-Forms & Components
 
In our 2-3 previous tutorials we have concentrated on the "basics" of Visual Basic command and syntax - enough so that programming with Visual Basic (easy enough for most situations) would not be any impediment as we start to develop more realistic applications. With this tutorial we return back to Forms and visual programming. We will learn to use VB's menu editor, one of the easiest ways to give your forms and programs not only the Windows look and feel but the GUI convenience of pulldown menus. It is interesting to note that more and more web sites are incorporating menu like structures despite the fact that in HTML or JavaScript there is no convenient way to create menus as easy as in VB or other languages like Delphi, Java, and . So without further to-do, lets return to VB Form(s).

Figure 1 - Vb.'s Menu Editor

VB's Menu Editor



In this program we will create a simple menu with File and Edit commands. Underneath the Edit menu we have four classic commands: Cut, Copy, Paste and Delete. However, we implement these commands in a general way so they can be used in any program or form. In order to do this we have to know the object naming conventions of VB as regards forms and the various controls/objects placed on them.

But first let us create a new project (call it menudemo and make it a standard .exe). The first thing we will do is create the menus. Actually, this is a good design principle - outline your program by defining the main menu items; then
the menus act as an skeleton of what your program is going to do. So our program is simple - File | Exit and Edit | Cut | Copy | Paste | Delete - are the complete commands.

To start the menu editor, click on the pulldown menu icon in the standard toolbar (shown above it is the third icon on the right). Figure 1 above shows a screen capture of the menu editor with most of the entries already filled in. We have just typed in the Caption - &Exit and the Name of the menu - exit. The Caption will appear on the form and the & indicates which letter (in this case the E in Exit) can be used as an ALT+key short cut to get at the menu item.

Now we will complete the Exit menuitem. See the four arrow buttons - they are used to place a menuitem in the correct position in the menu outline. In this case we need to move Exit down just below File but indented to the right. As you might guess, one mouse click on the menu editor's down arrow button and one click on the menu editor's right arrow key will do the trick. Note that for Exit's Shortcut key we have chosen CTRL+F4 from the Menu editor's ShortCut combo box. So now CTRL+F4 will cause the program to exit.

To program Exit, close the menu editor by clicking on OK. Note that your form now has the File and Edit menus. Click on File | Exit. Immediately you are brought into the VB editor at the following point:

Private Sub exit_Click()
|
End Sub

Type in "End" and you have created a proper VB exit from your program. As we shall shortly see, coding for menuitems is not always this simple; but remarkably about 10 to 20% of menuitems are just 1-5 lines of code. We shall now proceed to implement the Cut, Copy, Paste and Delete commands.

Objects for Cut, Copy, Paste, Delete

But before we do implement these commands we need to know two very useful things. All the objects and controls on a Visual Basic form have properties which, as we have seen before, are accessible through the Properties Sheet. These same design time properties are also accessible in your VB programs. The trick is knowing how to get at them. Table 1 provides some important pre-defined names in VB for accessing not just forms and controls but also application information. we shall take advantage of these pre-defined VB objects.

Table 1
Predefined VB Objects which are Programmable

VB Name of Object

Purpose of Object

Example

App currently active VB application App.Title
Clipboard the Windows Clipboard Clipboard.setText
Screen all forms and control currently active that VB knows Screen.ActiveForm
Screen.ActiveForm currently active form Screen.ActiveForm.Height
Screen.ActiveControl control on currently active form that has focus Screen.ActiveControl.Enabled

Remember, the way to refer to an oject's property is object.property. So lets change the height of our menudemo form programattically. Click on the Edit | Cut menuitem and add the one line of code as shown below:

Private Sub editop_Click(Index As Integer)
Screen.ActiveForm.Height = Screen.ActiveForm.Height + 200
End Sub

Run your program and click on Edit | Cut. The form will jump up in height every time you click on the Edit | Cut menuitem. You have just dynamically programmed your form. You can change almost any form property. Change the program by adding this line of code: Screen.ActiveForm.Caption = "I Did This" . Run your program and now the menu caption changes to "I Did This" and the form increases by 200 pixel in height when you click on Edit | Cut. We are now ready to program the Cut, Copy, Paste and Delete commands.


Figure 2 - Menudemo Program

To the form add two textboxes, a listbox, and a combobox as shown in Figure 2. The labels are optional. We are now going to program the commands so that when any text in a control is selected - you can cut, copy, paste or delete that text.

The programming trick is to make the program general by substituting for the specific: frmFonts.Text1.SelText the more general reference as: Screen.ActiveControl.SelText . By doing so, the Cut, Copy, Paste, and Delete commands will then work with any form and most controls that you use.
So add the following code to your program:

' Screen refers to the current form this could be
' referred to as Screen.ActiveForm
If TypeOf Screen.ActiveControl Is TextBox Or _
TypeOf Screen.ActiveControl Is ComboBox Then
Select Case Index
Case 1 ' Cut.
' Copy selected text onto Clipboard.
Clipboard.SetText Screen.ActiveControl.SelText
' Delete selected text.
Screen.ActiveControl.SelText = ""
Case 2 ' Copy.
' Copy selected text onto Clipboard.
Clipboard.SetText Screen.ActiveControl.SelText
Case 3 ' Paste.
' Put Clipboard text in text box.
Screen.ActiveControl.SelText = Clipboard.GetText()
Case 4 ' Delete.
' Delete selected text.
Screen.ActiveControl.SelText = ""
End Select
End If

Okay I admit it, there are some more programming tricks here. First, in order to simplify the programming I have used menu arrays so that I can then use a Select Case statement to do all the coding in one place. Look at Figure 1. Below the Caption and Name fields there is a third field called Index. Here is the trick. When I created the menuitems Cut, Copy, Paste, and Delete I gave them all the same Name => editop but different indexes: 1,2,3 and 4 respectively. Visual Basic then creates a menu array for me in which editop(1) is Cut, editop(2) is Copy etc. So now the Select Case Index will choose the correct line of code to goto depending on which menuitem is clicked. Nifty.

The second trick is to use the TypeOf ... Is expression. What this expression does is allow a programmer to identify the type of control being used in an expression. Thus TypeOf Screen.ActiveControl Is TextBox returns true if the current active control is a textbox otherwise it return false. This ability to distinguish the type of control is critical because if the active control isa command button or checkbox, then we want to to ignore it. Finally, we take advantage of the VB predefined object, Clipboard. VB provides a direct interface into Windows Clipoard and there are some more advanced things users can do - check the help files for details.


Summary

In this tutorial we have shown how to use the menu editor while creating generalized Cut, Copy, Paste and Delete commands. We have also reviewed some of the predefined VB objects. In the next tutorial we get our first taste of database and tryout a commercial OCX (store bought control).

Resources:

The book Beginning VB6/Wrox covers VB components on pages 589-620; VB6 Black Book/Coriolis touches on some of these topics on pages 970-975 and scattered throughout the book.
 
Top of Page  Tutorials Home