Programming an Application

Our next application is called MSVB_2, and. you will find it in the Example2 folder in this chapters folder on the CD. Start a new project, name the project MSVB_2, name its Form Examples, and then save it in its own folder.

Now, follow these steps:

1. When you are back in the Visual Basic IDE, select the Form and change its Caption property to TextBox Demo.

2. Place a textbox on the Form, as shown.

3. Select the textbox, and in the Properties window, set its MultiLine property to True.

By default, the TextBox control accepts a single line of text, and the default setting of its MultiLine property is False: By setting it to True, you are specifying that the textbox can accept multiple lines of text.

Since the textbox can hold multiple lines of text, it is possible for the user to enter text that exceeds the height of the control. To make sure that every piece of the text in the control is visible, you must set its ScrollBars property to Vertical. When you do so, ( vertical scroll bar appears on the right side of the control so that the user can scroll its contents up and down.

4. Locate the ScrollBars property in the Properties window, and expand the list of possible values by selecting the setting 2-Vertical. (The other possible values are O-None, L-Horizontal. and 3-Both.)

5. Add three Command buttons, and set their captions to Clear Text, Insert Date, and Show Message. We will program these buttons later to react when they are clicked.

6. From the Format menu, select the appropriate commands to make the size all three Command buttons the same, align their tops, and align them with the textbox.

The application’s interface is complete. Now run the application and check out the TextBox control. This control can add the basic functionality of a text editor’ to your applications. Notice how it wraps text as you type. Try out the editing keys. You can select text with the mouse and delete it, copy it to the Clipboard by pressing Ctrl+C, and paste it back by pressing Ctrl-V, You can even switch to another application, such as Notepad, copy some text with the other application’s Copy command, and then paste it in the textbox by pressing Ctrl+ V.

Programming the Command Buttons

Now we must add some code behind the Form’s buttons to perform the actions indicated. by their captions. Obviously, the Clear text button must clear the control by deleting any text on it.The Insert Date button displays the date in the control, and the Show Message button displays a message on the control. Follow these steps:

1. If the application is running, stop it by choosing Run >End.
2: When the Form appears on the screen, double-click the first Command button. Visual Basic opens the Code window, shown.

Command! is the name of the con trot and Click is the name of an event. This event is triggered every time the user clicks the first Command button. When this happens, Visual Basic looks for a subroutine named Commandl_Click, and if it’s found, it executes. In other words, if you want actions to execute every time the Command 1 control is clicked, you must insert them in the Command I_Click subroutine.

Here is the command to clear the TextBox control:

Textl. Text – ” “

Textl is the name of the control (it’s the default name Visual Basic assigned to it when it was created), and Text is the name of the property that sets (or reads) the text on the control. Setting the Text property to an empty string in effect clears the textbox. The Code window should now look like the one shown. Commandl_ClickO is an event handler, a procedure that specifies how the Commandl control must handle the Click event.

Visual Basic has inserted the following lines in the Code window and placed the pointer between them:

Private Sub Commandl_Click()
End Sub

Now double-click the second Command button to display its subroutine for the Click event. To display the current date in the textbox, the subroutine must contain the following line:

Private Sub Command2_Click()
Textl.Text – Date()
End Sub

The DateO function returns the current date, Assigning its value to the Text property replaces the current contents of the control with the date.

The Click event handler for the third button is:

Private Sub Command3_Click()
Textl.Text – ‘Welcome to Visual Basic 6.0·
End Sub

This subroutine displays the string “Welcome to Visual Basic 6.0” in the textbox.

Run the application and test the operation of the Command buttons. They perform simple operations, but their code is even Simpler. Each time you click the Insert Date or Show Message buttons, the current contents of the textbox are cleared. Ideally, we should be able to insert the date and message in the text, not replace it.

SelText

To insert a string in the textbox, you use the SeIText property, which represents the selected text in the control. If you assign a value to the SelText property,’ the selected text (not the entire text) is replaced with the new value. Even if no text is selected, the new string is inserted at the location of the pointer in the text. The , revised event handlers for the buttons Command! and coding are shown \ next, along with the rest of the code. Implement these changes, and then run the application to see how the commands behave now. You have some practical and useful features in your editor, which is based on an ActiveX control.

The Example2 Application

Grouping Controls

Our next application is called MSVB6-3, and its interface is shown. This example is another typical design with a peculiar requirement and demonstrates how to group controls.

The design of this Form is straightforward, Place eight Option buttons grouped in two blocks on the Form, set their captions as shown in Figure 1.16, and left-align each group’s controls by choosing Format> Align > Lefts. The titles are placed ‘on Label controls.

Run the application, and check out how the Option buttons work. Each time you click a control, the other controls.are cleared. But this isn’t how,this application should behave, is it? We want to be able to check one Option button in each column. By default, VISUalBasic checks one Option button only, no matter how many you have placed on the Form. it can’t group them without additional clues.

To create groups of controls, you place them on another control called a container. The most common choice for the container control is the Frame control. Option buttons placed on different frames form separate groups, arid Visual Basic maintains one checked Option button in each group. Let’s modify the application to create two groups of Option buttons, as shown . Follow these steps:

1. Select the Option buttons in the first group.
2. Choose Edit ;-..Cut. The Option buttons are removed from the Form and stored in the Clipboard. We will use the Paste command to place them back on the Form later.
3. Draw a frame large enough to hold the Option buttons you copied.
4. While the frame is selected, choose Edit >- Paste. The controls you removed from the Form earlier are pasted on the’ Frame control. They are not only grouped together, but they-are a permanent fixture of the Frame control. You can’t move them outside the frame.
5. Repeat steps 1 through 4 with the other four Option buttons. Select and copy them, place a second frame on the Form, and then paste. the controls on it. Align the buttons as needed,

As you can see, the two labels are no longer needed because the two frames have their own captions. Let’s delete the two labels and place the titles on the Frame controls. Follow these steps:

1. Select the first frame on the Form, and then locate its Caption property in the Properties window and set it to Select a Language.
2. Repeat step 1 for the second frame.
3. To turn on the bold attribute for both frames, select them-double-click their Font property in the Properties window, and in the Font dialog ,box click Bold.
4. To delete the labels that are no longer needed, select each one and press Delete, or select them both with the mouse and press Delete. You can also select both labels and choose Edit >- Delete.

Run the application now, and check out how the Option buttons behave. Each group has its own checked button, and you can select both a language and a browser. You cou!d have used. the P.i.ctureBoxcontrol instead of the Frame control.  I prefer the Frame, however, because it visually isolates the members of each group.  You can also ‘u~ a PictureBox control without a visible border so that the Option buttons appear ~ if they lie directly on the Form. They will behave as grouped buttons, just as they do when they are placed on a frame.

You now have an idea how Visual Basic works, and you have an understanding “” of the basic principles of application de’ “el(lpment with Visual Basic. The two main themes in developing applications with Visual Basic are:

• Visual design
• “Event-driven programming.

To design the user interface r-f the application, you don’t need m:ore than a basic understanding of the controls \a ud you know what each control does from your experience with other Window: applications) and basic drawing capabilities. If you have used drawing applications, you can design a user interface with
Visual Basic.

Visual Basic.
• Programming the application isn’t ,IS simple, but there’s a methodology you
.• can’t escape. A Visual Basic application isn’t a monolithic program such as applications you may have developed with other languages. When you program in VIsual Basic, you must first decide how the application interacts with the user. In • other words, you must decide how each control reacts to user actions, such as the ” click of a mouse, keystrokes, an 1so on, and you must program these reactions. This is called epe.nt-driven programming, because the application does not determine the flow; instead, the events caused by the user data: mine the fl~v of the application. You program the application to react to various external conditions (events), and the user’s actions determine the application s flow

Let’s put together the knowledge acquired so far and summarize the basic principles of application development.

Scroll to Top