Designing an Interface






Designing an Interface

It's generally best to design the user interface of a form 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 C# creates a new button and places it in the upper-left corner of the form (see Figure).

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 follows. 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 awhile.

Property

Value

Name

btnSelectPicture

Location

301,10 (Note: 301 is the x coordinate; 10 is the y coordinate.)

Size

85,23

Text

Select Picture


You're now going to 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 button and choose Copy from its shortcut menu. Next, right-click anywhere on the form and choose Paste from the form's shortcut menu (you could have also used 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 retained almost all of 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

301,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 in Figure. Click the Save All button on the toolbar to save your work.

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


Adding an Invisible Control to a Form

All the controls that you've used so far sit on a form and have a physical appearance when the application is run. Not all controls have a physical appearance, however. Such controls, referred to as 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 C# 2005.

To enable the user to select a picture to display, you need to give them the ability to locate a file on their hard drive. 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 the OpenFileDialog control, and it will save you dozens and dozens of hours that would otherwise be necessary to duplicate this common functionality.

By the Way

Other controls in addition to the OpenFileDialog control give you file functionality. For example, the SaveFileDialog control provides features for allowing the user to specify a filename and path for saving a file.


Display the toolbox now 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, but rather it appears in a special area below the form (see Figure). This happens because the OpenFileDialog control has no form interface to display to a 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.

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

<delete existing text and make empty>

Filter

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

Title

Select Picture


The Filter property is used to limit (Filter) 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 by 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.



 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows