• No results found

Examples

In document Visual Basic. en.wikibooks.org (Page 96-104)

An example form using file reading can be found here.1 Right click 'here' in Internet Explorer and then 'Save Target As...' to save the form file.

Please upload more files with examples. It will be really helpful for the developer.

17 User Interfaces

A user interface is the part of your program that is visible to a human user. It can be as simple as a command line1or as sophisticated as an immersive virtual reality simulator (see Death Dream by Ben Bova2). However, in the context of Visual Basic it usually means what is commonly referred to as a Graphical User Interface or GUI which generally consists of one or more Forms that contain text boxes, labels, buttons, picture boxes, etc.

A more common example of a GUI would be a standard EXE( Windows Forms3).

1 http://en.wikibooks.org/wiki/Guide%20to%20Windows%20commands 2 http://www.ibdof.com/IBDOF-book-detailedview.php?book_id=5023 3 http://en.wikipedia.org/wiki/Windows_Forms

18 Simple Graphics

This chapter is a simple introduction to drawing simple graphics in Visual Basic Classic. It is possible to do very sophisticated graphics programming in VB but here we will concentrate of the basics.

There are two ways of creating simple two dimensional graphics in VB:

• draw lines, boxes, circles, and ellipses at run time by executing statements or • add shape objects to the form at design time.

If you know in advance exactly what to draw, then it might be simpler to use the second method, as you can then see exactly what you will get as you drag and drop shapes to the form. However if the picture is to be generated based on, say, some statistics, you might be better off using the drawing methods of a Form or PictureBox.

18.1 Shape and Line controls

There are two built-in controls in Visual Basic that can be used to create simple two- dimensional drawings:

• Shape • Line

Here is a simple example showing some of the shapes that can be created simply by dropping Shape controls on a Form:

Simple Graphics

Figure 3

Note that you can modify the properties of Shape and Line controls at run time just as you can any other control. You can also create them at run time, either by using Control

19 Windows Dialogs

Windows dialogs are useful when you would like to retrieve information from the user, as in asking where to save a file, letting the user to choose a color or font and what settings to use when printing (copies, what printer to use, ect.). This saves you from a lot of unnecessary work reinventing the wheel - as redesigning and making dialogs can be quite time-consuming - but is also essential in establishing a standard user interface across applications. Stan- dardization of different aspects of the user interface, both in terms of normal widgets1 as well as common ways of combining them, ensures that applications are intuitive - i.e. that once someone has learned to use an application, this knowledge can be employed to other applications as well.

The easiest way to access these controls is to use a component called Microsoft Common

Dialog Control 6.0. To add this component to your project, choose Project - Components,

and mark it in the controls list. Subsequently, a new control should appear in your control toolbox. Choose it, and place the control onto your form (note it's only visible in design time).

The control allows the use of the following dialogs. The flags mentioned is different values that can be set to the Flags property, changing the behaviour or design of the different dialogs.

19.1 Open and Save dialogs

ShowOpen shows a dialog that lets the user choose a drive, directory, file name and file

extension to (presumably) open a file.

The save dialog is identical in apperance and function, with the exception of the caption. At run time, when the user chooses a file and closes the dialog box, the FileName property is used to get the selected file name.

Windows Dialogs

Figure 4

19.1.1 Before displaying the dialog

Before you can use these dialogs, you must first set the Filter property to allow the user to select a file extension (set default extension using DefaultExt). The filter property is a string with the following structure:

description1|filter1|description2|filter2|

Here, the description is the string that shows up in the list box of all the available filters to choose from. The filter is a file glob expression (otherwise known as a wild card expression) that does the actually filtering, for example, "*.txt", which disallows any file extensions but TXT. It is important to recognize the difference between expressions of this kind and regular expressions which are much more powerful but also more complicated, see ../Regular Expressions/2.

19.1.2 Code example

On Error Resume Next

' Clear errors

Err.Clear

' Use this common dialog control throughout the procedure

Color dialog

With CommonDialog1

' Raises an error when the user press cancel

.CancelError = True

' Set the file extensions to allow

CommonDialog1.Filter = "All Files (*.*)|*.*|TextFiles (*.txt)|*.txt|Batch Files (*.bat)|*.bat"

' Display the open dialog box.

.ShowOpen

' Ignore this if the user has canceled the dialog

If Err <> cdlCancel Then

' Open the file here using FileName

MsgBox "You've opened '" & .FileName & "'"

End If

End With

In document Visual Basic. en.wikibooks.org (Page 96-104)