The VCL, Forms, and Components

   

The VCL, Forms, and Components

The Visual Component Library (VCL) is the source of many of the components used to create C++Builder applications (there are cross-platform components in a similar library called CLX). A component is an object, often visual, such as a check box, a drive combo box, or a graphical image. Components can also be nonvisual, such as database connections or sockets for distributed system communication. Components are chosen from the component palette of the IDE by left-clicking and placing them in your work area. See the section "The Component Palette," later in this chapter, and Figure 1.1, which shows the windows of the IDE.

You can also add or write your own components, as discussed in upcoming chapters. Whether you are creating new components or using existing ones, the components that make up the VCL remove most of the hard work for you.

All components have properties, whose values can be modified at design time in the development environment. You can modify the properties of a component with the Object Inspector (refer to Figure 1.1 to see the window). You can also change property settings in code, but this should be avoided wherever possible.

The Object Inspector has an Events tab, where you can attach code to events that occur during user interaction with the component. These event handlers make up the bulk of the code in a C++Builder program.

The Form

A form is a visible window that is part of the user interface of your application. When you create a new application in C++Builder, a blank main form is created automatically. To build your user interface, simply add visual components to the form, then position and size them accordingly . You can also add nonvisual components to a form, such as timers. These appear as a simple component icon at design time, but are not visible at runtime. You can create specialized forms for tool windows or dialogs.

By default, when a user runs your application the main form will be displayed where you placed it on the screen in the IDE. You can alter the initial position of the form and other settings by changing the form properties in the Object Inspector.

The Component Palette

The Component Palette, located under the main menu, is an inventory of all the components in the VCL. These components are grouped by categories named on the tabs above the components. To pick a component, click it with your left mouse button, then click again on the form to place the component where you want it. As indicated previously, you can modify the component's properties with the Object Inspector. You can also change visual components by dragging their edges to resize them, or dragging the component to reposition it.

Events and Event Handlers

As a first lesson on using C++Builder, you can place a simple button on the form, set an event for the button, and run the program. You see buttons on almost every Windows application. It is a simple object that enables a user to trigger an event handler in the program.

The Button component is located under the Standard tab of the Component Palette. Its icon looks like a standard button with OK in the middle. To place a Button component on the form, left-click the icon one time, then left-click again on the center of the form. Figure 1.3 shows the form with the button.

Figure 1.3. A Button component added to the form.

graphics/01fig03.gif

C++Builder has now created an instance of a button as part of your program. At the moment the button isn't very useful because it doesn't do anything. If you were to compile and run the application, nothing would happen as a result of a click of the button. Typically though, the event handler will perform some action, such as saving information the user has entered, displaying a message, or any one of thousands of other possibilities.

When the button is clicked at runtime, an event is generated. For your application to respond to this event you need to have coded an event handler. An event handler is a function that is automatically called when its event occurs. C++Builder creates the outline for the OnClick event handler, the event that occurs when the user clicks the button, when you double-click the Button component at design time. You can do the same thing by selecting the Button, and then double-clicking in the OnClick field on the Events tab in the Object Inspector. After the outline for the event handler is created, you can then add code to perform the necessary action that should occur when the button is clicked.

The outline for an OnClick event handler is shown in the following code.

 void __fastcall TForm1::Button1Click(TObject *Sender)  {  } 

If you right-click with the mouse in the Code Editor and choose Open Source/Header File, you will see the following code:

 //---------------------------------------------------------------------------- #ifndef Unit1  #define Unit1  //---------------------------------------------------------------------------- #include <Classes.hpp>  #include <Controls.hpp>  #include <StdCtrls.hpp>  #include <Forms.hpp>  //---------------------------------------------------------------------------- class TForm1 : public TForm  {  __published:    // IDE-managed Components          TButton *Button1;          void __fastcall Button1Click(TObject *Sender);  private:    // User declarations  public:        // User declarations          __fastcall TForm1(TComponent* Owner);  };  //---------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1;  //---------------------------------------------------------------------------- #endif 

This code is generated automatically within C++Builder. I put it here to show you what C++Builder can do for you automatically.

Now, we'll add code to display a message when the button is clicked, that the program is going to end, and then we'll terminate the program. In the code editor, click Unit1.cpp to move back to the Button component's event. Type the following code inside the event that you just created:

 void __fastcall TForm1::Button1Click(TObject *Sender)  {      ShowMessage("Hello world! This is a test application! Press OK");      Close();  } 

Even a beginner can probably understand this code. When the program runs, the user clicks the button and an event is triggered. Your OnClick event handler will display a dialog box with your friendly message. When the user closes the dialog box our program then terminates because of the Close() call made to the form.

Testing the Program

From the IDE toolbar, click the green arrow that looks like the Play button of a tape player ”this is the Run button. When you press this button, C++Builder will start to compile and execute the program. It then waits until you press the button. When you do so, the dialog box appears revealing the message. The program exits when you press OK.

After viewing your program, from the main menu, choose File, New Application. C++Builder will ask if you want to save the application project you were working on; answer No. Next will be your chance to write a real program that can do something.


   
Top


C++ Builder Developers Guide
C++Builder 5 Developers Guide
ISBN: 0672319721
EAN: 2147483647
Year: 2002
Pages: 253

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net