WORKING WITH COMPONENTS
Delphi For Beginners:
An introduction to Delphi VCL and a simple application.
As you probably know by now, components are essential elements of the Delphi environment. It's crucial for every Delphi beginner to understand what VCL has to offer.
Prior to understanding this article it will be good idea to read What is Delphi, and Delphi for Beginners: Getting started.
First of all, let's distinguish components from controls. Simply putt controls are those components that the user can see. Furthermore, Delphi distinguishes between windowed and nonwindowed (graphical) controls. The major distinction is that windowed controls can receive the focus and nonwindowed controls cannot.
The distinction between windowed and graphical controls can be important when you design your user interfaces. For example, if you create a tool bar that contains a large number of buttons, you could choose a nonwindowed speed-button component instead standard Windows button components. Non graphical controls consume fewer system resources.
Some components are visual components; others are nonvisual components. A visual component, is one that can be seen by the user at run time. Visual components include components such as edit controls, buttons, list boxes, labels, and so on. A nonvisual component is one that cannot be seen by the user at run time (system timers, database components, image lists, etc.) When you place a nonvisual component on a form, Delphi displays an icon representing the component on the form. This icon is used to access the component at design time in order to change the component's properties, but the icon does not show up when the program runs.
Depending on which version of Delphi you have, you start with more than 100 components. Each page tab in the Component palette displays a group of icons representing the components you can use to design your application interface.
Each component has a set of properties such as color, size, position, caption that can be modified in the Delphi IDE or in your code, and a collection of events such as a mouse click, keypress, or component activation for which you can specify some additional behavior (in event procedures).
Adding components to the form
To place a component on the form, click once on the desired component on the toolbar. Then move the mouse cursor over to the Form and click on the Form where you want the component to be. Repeat the same procedure with the rest of the components you wish to use.
Of course, you may want a component to be smaller or larger than the default size. To change the dimensions of the component simply stretch it using the mouse (in a way you deform any standard window). Take a moment to see that Height and Width property of that controls changes in the Object Inspector.
To move a component to a different location simply select a component (click on it) and drag it to its new location. Again, you are changing properties: Left, Top.
If you would like to cancel a design-time drag operation you've already begun, do the following: after you've begun the drag but before you release the mouse button, press the Esc key. The control will snap back to its original position!
Besides the form itself, Delphi provides several components-such as the group box, panel and page control that can contain other components. The idea of a container is that all the components will behave as one at design time. For example when you move a container component, the child components move with it. Once you have placed container component on the form, make sure that it is selected than add child components to the container as you normally would.
Simple application My Second Delphi Program
This simple application will show you how to change some of the standard component properties at design and run-time.
When you change a component's properties through the Object Inspector and Form Designer, you are said to make a design-time change. When you modify a property through code that executes when the program runs, you are said to make a runtime change.
We will build our application in few steps, so here they are:
|0.||Start Delphi, and as we know, you are presented with both a blank form and the IDE.|
Put one Label (TLabel is a nonwindowed control that displays text on a form.), Edit (TEdit is a standard Windows edit control) and a Button on the form -all components are on the Standard Page of components palette
|2.||Change form caption to 'My Second Delphi Program', Button caption to 'Copy text'. (this is not necessary for application we are just design-time changing properties)|
The idea is to change the text in Label component to the text in Edit component when the user clicks on the Button component.
|3.||Select the Button and click on the Events tab in the Object Inspector window. Double click in the right column of the item marked "OnClick".|
4. Enter this code (marked red).
|5.||Now, run the project (F9), and notice that every time you click on the button, text in Label component changes to the text in Edit box. Of course, try to change the text in Edit box before clicking on the Button to see the results.|
In that line of code, that we had to write down, is everything we need to accomplish desired task. We are run-time changing the Caption property of the Label so that it's the same as the Text property of the EditBox.
When you assign a value to a property, the value on the right side of the assignment statement is assigned to the property on the left side of the assignment statement. When a value is assigned, a copy of the value is placed into the property.
The idea of this article was to bring you closely the complexity of Delphi VCL. I hope you will find it useful. Of course, if you have any questions or comments don't hesitate to mail me. In some of the future articles I will be writing about Database Development for Delphi beginners and other features that will help you to better understand Delphi Programming.