Share via


Visual Basic Concepts

Designing a Form

Form objects are the basic building blocks of a Visual Basic application, the actual windows with which a user interacts when they run the application. Forms have their own properties, events, and methods with which you can control their appearance and behavior.

Figure 3.2   Forms and controls have their own properties, events, and methods

The first step in designing a form is to set its properties. You can set a form's properties at design time in the Properties window, or at run time by writing code.

Note   You work with forms and controls, set their properties, and write code for their events at design time, which is any time you're building an application in the Visual Basic environment. Run time is any time you are actually running the application and interacting with the application as the user would.

Setting Form Properties

Many of a form's properties affect its physical appearance. The Caption property determines the text that is shown in the form's title bar; the Icon property sets the icon that is displayed when a form is minimized. The MaxButton and MinButton properties determine whether the form can be maximized or minimized. By changing the BorderStyle property, you can control the resizing behavior of the form.

Height and Width properties determine the initial size of a form; Left and Top properties determine the form's location in relation to the upper left-hand corner of the screen. The WindowState property can be set to start the form in a maximized, minimized, or normal state.

The Name property sets the name by which you will refer to the form in code. By default, when a form is first added to a project, its name is set to Form1, Form2, and so forth. It's a good idea to set the Name property to something more meaningful, such as "frmEntry" for an order entry form.

The best way to familiarize yourself with the many form properties is to experiment. Change some of the properties of a form in the Properties window (Figure 3.3), then run the application to see their effect. You can learn more about each property by selecting it and pressing F1 to view the context-sensitive Help.

Figure 3.3   The Properties window

Form Events and Methods

As objects, forms can perform methods and respond to events.

The Resize event of a form is triggered whenever a form is resized, either by user interaction or through code. This allows you to perform actions such as moving or resizing controls on a form when its dimensions have changed.

The Activate event occurs whenever a form becomes the active form; the Deactivate event occurs when another form or application becomes active. These events are convenient for initializing or finalizing the form's behavior. For example, in the Activate event you might write code to highlight the text in a particular text box; in the Deactivate event you might save changes to a file or database.

To make a form visible, you would invoke the Show method:

Form2.Show

Invoking the Show method has the same effect as setting a form's Visible property to True.

Many of a form's methods involve text or graphics. The Print, Line, Circle, and Refresh methods are useful for printing or drawing directly onto a form's surface. These methods and more are discussed in "Working with Text and Graphics."

For More Information   For additional information on forms, see "More About Forms" in "Creating a User Interface."