|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98
Chapter 4 Managing Forms In Visual Basic
- If you need an immediate solution to:
- Setting Title Bar Text
- Adding/Removing Min/Max Buttons And Setting A Windows Border
- Adding Toolbars To Forms
- Adding Status Bars To Forms
- Referring To The Current Form
- Redrawing Form Contents
- Setting Control Tab Order
- Moving And Sizing Controls From Code
- Showing And Hiding Controls In A Form
- Measurements In Forms
- Working With Multiple Forms
- Loading, Showing, And Hiding Forms
- Setting The Startup Form
- Creating Forms In Code
- Using The Multiple Document Interface
- Arranging MDI Child Windows
- Opening New MDI Child Windows
- Arrays Of Forms
- Coordinating Data Between MDI Child Forms (Document Views)
- Creating Dialog Boxes
- All About Message Boxes And Input Boxes
- Passing Forms To Procedures
- Minimizing/Maximizing And Enabling/Disabling Forms From Code
In Depth
In this chapter, well take a look at handling forms in Visual Basic. Theres a great deal to see about form handling, and well look at it all. Well see how to customize forms, how to work with multiple forms, how to support the multiple document interface (MDI), how to coordinate MDI child forms, how to use the MsgBox() and InputBox() functions, how to load, hide, show, and unload forms, and much more. Well begin the chapter by getting an overview of Visual Basic forms.
The Parts Of A Form
Forms are the names for windows in Visual Basic (originally, you called windows under design forms, and the actual result when running a window, but common usage has named both forms now), and you add controls to forms in the Integrated Development Environment (IDE).
Were designing a form in the Visual Basic IDE in Figure 4.1, and you can see several aspects of forms there. At the top of the form is the title bar, which displays the forms title; here thats just Form1. At right in the title bar is the control box, including the minimizing/maximizing buttons and the close button. These are controls the user takes for granted in most windows, although well see they are inappropriate in others (such as dialog boxes).
Figure 4.1 A form under design.
Under the title bar comes the menu bar, if there is one. In Figure 4.1, the form has one menu: the File menu (well see how to work with menus in the next chapter). Under the menu bar, forms can have toolbars, as you see in the IDE itself.
The main area of a formthe area where everything takes placeis called the client area. In general, Visual Basic code works with controls in the client area and leaves the rest of the form to Visual Basic (in fact, the client area is itself a window). In Figure 4.1, weve added a controla command buttonto the form.
Finally, the whole form is surrounded by a border, and there are several types of borders that you can use.
The Parts Of An MDI Form
Besides standard forms, Visual Basic also supports MDI forms. An MDI form appears in Figure 4.2.
Figure 4.2 An MDI form.
You can see that an MDI form looks much like a standard form, with one major difference, of coursethe client area of an MDI form acts like a kind of corral for other forms. That is, an MDI form can display MDI child forms in it, which is how the multiple document interface works. In Figure 4.2, we have two documents open in the MDI form.
Thats the third type of form you can have in Visual BasicMDI child forms. These forms appear in MDI child windows, but otherwise are very similar to standard forms.
Those, then, are the three types of forms available to us in Visual Basic: standard forms, MDI forms, and MDI child forms. Well work with all of them in this chapter. In fact, were ready to start getting into the details now as we turn to the Immediate Solutions section of this chapter.
Immediate Solutions
Setting Title Bar Text
Youve submitted your project to the user-testing stage and feel smug. What could go wrong? Suddenly the phone ringsseems they dont like the title in the programs title bar: Project1. How can you change it?
This stymies a lot of Visual Basic programmers, because the text in the title bar seems like something that Windows itself manages, not the program. In fact, its up to the program, and setting the text in the title bar couldnt be easier. At design time, you just change the forms Caption property, as shown in Figure 4.3.
Figure 4.3 Setting a forms caption.
You can also set the Caption property at runtime in code like this (note that we use the Me keyword here to refer to the current formsee Referring to the Current Form later in this chapter):
Private Sub Command1_Click()
Me.Caption = "Hello from Visual Basic!"
End Sub
Adding/Removing Min/Max Buttons And Setting A Windows Border
Forms usually come with minimizing and maximizing buttons, as well as a close box at the upper right. However, thats not appropriate in all cases, as well see when we design dialog boxes later in this chapter.
To remove these buttons, you can set the forms ControlBox property to False, as shown in Figure 4.4. Note that the usual buttons are missing from the form at the upper right.
Figure 4.4 Removing the control box from a form.
TIP: If you are thinking of designing a dialog box, take a look at Creating Dialog Boxes later in this chapterbesides removing the control box, you should also set the dialogs border correctly, add OK and Cancel buttons, and take care of a few more considerations.
You can also set what buttons are in a form by setting its border type. For example, if you set the border style to a fixed type, the minimizing and maximizing buttons will disappear.
Setting A Forms Border
You set a forms border style with its BorderStyle property; here are the possible values for that property:
- 0None
- 1Fixed Single
- 2Sizable
- 3Fixed Dialog
- 4Fixed Tool window
- 5Sizable Tool window
Well see more about using the BorderStyle property when we work with dialog boxes in this chapter.
|