Sign In
Link to us
Home
1. Introduction
2. Introducing VB
3. Projects
4. Containers
 4.1 Forms
 4.2 Frame & PictureBox

5. Controls
6. Code
7. Forms
8. Debugging Code
9. Error Handling
10. Objects
11. User Controls
12. Data Projects
13. API
14. Distribution

Other Resources


Affiliated Links:
www.freefunfings.com


4. Containers

A control is an interactive element that can be placed onto a window, such as a button, text box or picture. Controls are usually placed straight onto a window from the toolbox (see earlier). Some Controls can hold other Controls - these are called Container controls.

The Control which contains the other Controls is said to be the Parent Control, whilst the Controls which are held by the Container are said to be Child Controls.

In the picture above, "Form1" is a container for "Frame1" and the "Exit" button. Note that "Frame1" is a container for the text box, "Text1" and the "OK" button.

Child Controls are grouped together inside the Parent Control. E.g. If a Control is a Container, it's Child Controls will move when it moves. For example, if "Frame1" in the picture above was moved, "Text1" and the "OK" button would move with it.

4.1 Forms

The simplest way to describe a Form is as a window. When using Windows, almost all of the user-interface is done through Forms (with exceptions such as the desktop and the system tray). A Form is not itself a control.

A Form is an object that can contain other controls, such as buttons and lists - it is a Container object. When developing an application, the graphical elements can be broken down into areas of concern and placed on separate Forms. These Forms can then be opened and closed as required during the execution of the application.

Exercise iv:
  • Create a new "Standard EXE" project in Visual Basic, as shown previously.
  • Add a new Form into the project by selecting "Add Form" from the Project menu and selecting "Form". In the Project Explorer window, expand the "Forms" folder and verify that you have two forms.
  • Double-click on Form1 in the Project Explorer. The form should appear in the development area (if not already there).
  • Select the CommandButton from the Toolbox. Drag an area of the Form where you want the new CommandButton to be.
  • Double-click the new CommandButton. The MicrosoftT Visual Basic code window will open and the cursor will prompt you to type in some code for the CommandButton's "Click" event. Type in the following:

Private Sub Command1_Click()
    Form2.Show 1
End Sub
  • Open "Form2" and add a CommandButton in the same way. Double-click the CommandButton and type:

Private Sub Command1_Click()
    Unload Me
End Sub
  • Start the Project by selecting "Start" in the Run menu or by pressing F5
  • When the first form appears (Form1) click the CommandButton. This runs the code you typed for that button - i.e. It shows Form2. Now click the CommandButton on Form2. This runs the code you typed on the second button - i.e. the Form unloads itself.
This exercise demonstrates how Forms can be opened and closed.

4.2 Frame and PictureBox Controls

Frames and PictureBox Controls are also Containers, however, they must themselves be placed onto a Container (such as a Form or User Control). We will cover Frame and PictureBox controls later in the course.

Once the Frame or PictureBox is on a Form, it can have other Controls added to it.

Exercise v:
  • Re-open the previous Forms project that you did, remembering to save a copy.
  • Draw a Frame on Form1. We are now going to move the CommandButton so that it is Contained by the Frame.
  • Select the CommandButton and cut it by pressing Ctrl+X. Make sure that the Frame is selected by clicking it, then paste the CommandButton back by pressing Ctrl+V.
Note:  There is no need to change the Visual Basic code associated with a Control when you move it, provided that you do not change the (Name) property of that control.
  • Enlarge the Form if necessary and move the Frame around the form by dragging it. Notice that the CommandButton now moves with the Frame.
  • Open the second Form and repeat the above using a PictureBox Control instead of a frame. Again, move the CommandButton into the PictureBox.
This demonstrates how a Control is held inside a Container.

<<Page 3  :   Top  :   Page 5.1>>
©vbtraining.co.uk