Home > Articles > Programming > Visual Basic

  • Print
  • + Share This
This chapter is from the book

Designing an Interface

It's generally best to design a form's user interface and then add the code behind the interface to make the form functional. You'll build your interface in the following sections.

Adding a Visible Control to a Form

Start by adding a Button control to the form. Do this by double-clicking the Button item in the toolbox. Visual Basic creates a new button and places it in the upper-left corner of the form, as shown in Figure 1.9.

Figure 1.9

Figure 1.9 When you double-click a control in the toolbox, the control is added to the upper-left corner of the form.

Using the Properties window, set the button's properties as shown in the following list. Remember, when you view the properties alphabetically, the Name property is listed first, so don't go looking for it down in the list, or you'll be looking a while.

Property

Value

Name

btnSelectPicture

Location

295,10 (295 is the x coordinate; 10 is the y coordinate.)

Size

85,23

Text

Select Picture

Now you'll create a button that the user can click to close the Picture Viewer program. Although you could add another new button to the form by double-clicking the Button control on the toolbox again, this time you'll add a button to the form by creating a copy of the button you've already defined. This allows you to easily create a button that maintains the size and other style attributes of the original button when the copy was made.

To do this, right-click the Select Picture button, and choose Copy from its context menu. Next, right-click anywhere on the form, and choose Paste from the form's shortcut menu. (You can also use the keyboard shortcuts Ctrl+C to copy and Ctrl+V to paste.) The new button appears centered on the form, and it's selected by default. Notice that it retains almost all the properties of the original button, but the name has been reset. Change the properties of the new button as follows:

Property

Value

Name

btnQuit

Location

295,40

Text

Quit

The last visible control you need to add to the form is a PictureBox control. A PictureBox has many capabilities, but its primary purpose is to show pictures, which is precisely what you'll use it for in this example. Add a new PictureBox control to the form by double-clicking the PictureBox item in the toolbox, and set its properties as follows:

Property

Value

Name

picShowPicture

BorderStyle

FixedSingle

Location

8,8

Size

282,275

After you've made these property changes, your form will look like the one shown in Figure 1.10. Click the Save All button on the toolbar to save your work.

Figure 1.10

Figure 1.10 An application's interface doesn't have to be complex to be useful.

Adding an Invisible Control to a Form

All the controls you've used so far sit on a form and have a physical appearance when a user runs the application. Not all controls have a physical appearance, however. Such controls, called nonvisual controls (or invisible-at-runtime controls), aren't designed for direct user interactivity. Instead, they're designed to give you, the programmer, functionality beyond the standard features of Visual Basic.

To enable users to select a picture to display, you need to give them the ability to locate a file on their hard drives. You might have noticed that whenever you choose to open a file from within any Windows application, the dialog box displayed is almost always the same. It doesn't make sense to force every developer to write the code necessary to perform standard file operations, so Microsoft has exposed the functionality via a control that you can use in your projects. This control is called OpenFileDialog, and it will save you dozens of hours that would otherwise be necessary to duplicate this common functionality.

Display the toolbox and scroll down using the down arrow in the lower part of the toolbox until you can see the OpenFileDialog control (it's in the Dialogs category), and then double-click it to add it to your form. Note that the control isn't placed on the form; rather, it appears in a special area below the form (see Figure 1.11). This happens because the OpenFileDialog control has no form interface to display to the user. It does have an interface (a dialog box) that you can display as necessary, but it has nothing to display directly on a form.

Figure 1.11

Figure 1.11 Controls that have no interface appear below the form designer.

Select the OpenFileDialog control, and change its properties as follows:

Property

Value

Name

ofdSelectPicture

Filename

<make empty>

Filter

Windows Bitmaps|*.BMP|JPEG Files|*.JPG

Title

Select Picture

The Filter property is used to limit the types of files that will be displayed in the Open File dialog box. The format for a filter is description|filter. The text that appears before the first pipe symbol is the descriptive text of the file type, whereas the text after the pipe symbol is the pattern to use to filter files. You can specify more than one filter type by separating each description|filter value with another pipe symbol. Text entered into the Title property appears in the title bar of the Open File dialog box.

The graphical interface for your Picture Viewer program is now finished. If you pinned the toolbox open, click the pushpin in the title bar of the toolbox now to close it.

  • + Share This
  • 🔖 Save To Your Account