1. Tech

Your suggestion is on its way!

An email with a link to:


was emailed to:

Thanks for sharing About.com with others!

Learn about: properties, events and Delphi Pascal
Page 2: Changing Component Properties; Writing Code - Events and Event Handlers
 More of this Feature
• Page 1: Placing Components on a Form

Printer friendly versionPrinter friendly version
 Join the Discussion
"Post your views and comments to this chapter of the free Delphi Programming Course"
 Related Resources
• A Beginner's Guide to Delphi Programming.TOC

   Changing Component Properties
After you place components on a form, you can set their properties with the Object Inspector. The properties are different for each type of component, some properties apply to most components. Altering a component property, changes the way a component behaves and appears in an application.
All the components have a property called "Name". The Name property is very important; it specifies the name of the component as referenced in code. When you first place a component on a form, Delphi will provide a default name for the component: Label1, Edit1, Button1. I suggest you to give your components a meaningful name before writing the code that refers to them. You can do this by changing the value of the Name property in the Object Inspector. Here are some suggestions on how to give names to components.

Note: with the last statement in mind, I'll do the opposite. In most cases, I'll leave all the default component names through this Course - just as they appear when you place them on a form.

To actually change a component property you first need to activate it - click it to select it - small square handles appear at each corner and in the middle of each side. Another way to select a component is to click its name in the drop down list that appears at the top of the Object Inspector. This list lists all the components on the active form along with their types in the following format: "Name     Type".

When a component is selected, its properties (and events) are displayed in the Object Inspector. To change the component property click on a property name in the Object Inspector; then either type a new value or select from the drop-down list.
For example, change the Caption property for Button1 (I'll refer components by their names) to 'Hello...' (of course without the single quotation marks)

Button, Caption property in Object Inspector

Components have different kinds of properties; some can store a boolean value (True or False), like Enabled. To change a boolean property double click the property value to toggle between the states. Some properties can hold a number (e.g. Width or Left), a string (e.g. Caption or Text) or even a set of "simple valued" properties. When a property has an associated editor, to set complex values, an ellipsis button appears near the property name. For example if you click the ellipsis of the Font property a Font property dialog box will appear.

Now, change the Caption (the static text the label displays on the form) of Label1 to 'Your name please:'. Change the Text property (text displayed in the edit box - this text will be changeable at run time) of Edit1 to 'Zarko Gajic' (this is my name, write your name).

   Writing Code - Events and Event Handlers
To really enable components to do something meaningful you have to write some action-specific code for each component you want to react on user input. Remember: components are building block of any Delphi form, the code behind each component ensures a component will react on an action.

Each Delphi component, beside its properties, has a set of events. Windows as even-led environment requires the programmer to decide how a program will (if it will) react on user actions. You need to understand that Windows is a message-based operating system. System messages are handled by a message handler that translates the message to Delphi event handlers. For instance, when a user clicks a button on a form, Windows sends a message to the application and the application reacts to this new event. If the OnClick event for a button is specified it gets executed.

Object Inspector Component Events The code to respond to events is contained in Delphi event procedures (event handlers). All components have a set of events that they can react on. For example, all clickable components have an OnClick event that gets fired if a user clicks a component with a mouse. All such components have an event for getting and loosing the focus, too. However if you do not specify the code for OnEnter and OnExit (OnEnter - got focus; OnExit - lost focus) the event will be ignored by your application.

To see a list of events a component can react on, select a component and in the Object Inspector activate the Events tab. To really create an event handling procedure, decide on what event you want your component to react, and double click the event name.

For example, select the Button1 component, and double click the OnClick event name. Delphi will bring the Code Editor to the top of the screen and the skeleton code for the OnClick event will be created.

procedure TForm1.Button1Click(Sender: TObject);
//this is where your code goes

Note: For the moment there is no need to understand what all the words in the above code stand for. Just follow along, we'll explain all that in the following chapters.

As you will understand more clearly through this course, a procedure must have a unique name within the form. The above procedure, Delphi component event-driven procedure, is named for you. The name consists of: the name of the form (prefixed with T) "TForm", a full stop ".", the component name "Button1", and the event name "Click". For any component there is a set of events that you could create event handlers for. Just creating an event handler does not guarantee your application will do something on the event - you must write some event handling code in the body of the procedure.

A few words on Delphi (Object) Pascal
The code you write inside event procedures is Pascal code. Object Pascal or Delphi Pascal (as I will mostly call it), a set of object-oriented extensions to standard Pascal, is the language of Delphi. Delphi Pascal enables you to take advantage of object-oriented programming to its fullest. It can be said that Delphi Pascal is to Pascal what C++ is to C. As Delphi was being developed, new language behavior and keywords were added to deal with the component model. In general, Delphi Pascal is a high-level, compiled, strongly typed language that supports structured and object-oriented design.

We'll now write some code for the OnClick event handler of Button1. Alter the above procedure body to:

procedure TForm1.Button1Click(Sender: TObject);
 var s: string;
 s := 'Hello ' + Edit1.Text + ' Delphi welcomes you!';

A few words on Delphi Code completion
Code Completion When you reach to the second line and write "Edit1." wait a little, Delphi will display a list box with all the properties of the edit box you can pick. In general, it lists valid elements that you can select from and add to your code. Here's more info on Delphi Code Insight tools.

Now, hit F9 to compile and run your project. When the program starts, click the Button1 ('Hello...'). A message box will pop up saying 'Hello Zarko Gajic, Delphi welcomes you!'. Change the text in the Edit box and hit the Button again...

Hello you at run time

What follows is a simple explanation of the code that runs this application. Let's see.

  • The first line under the procedure name, var s: string;, declares a string type variable. Variables in Delphi Pascal hold information (values). Variables have to be declared before they can be used. We do this after the var keyword.
  • The first line under the begin keyword, s := 'Hello ' + Edit1.Text + ' Delphi welcomes you!'; sets a value for the variable s. This assignment involves reading a value of the Text property for the Edit component. If you ask Delphi about the Text property of an Edit component, you'll find out that it holds the text string that is displayed in the edit box. That text is of the TCaption type, actually the string type.
  • The last statement, before the end keyword, ShowMessage(s);, is the one that calls the message dialog and sends it the value of variable s - this results in a pop up box your see above.

    That's it. Again, not too smart, not too hard but serves the purpose. By now you should know how to place components on a form, set their properties and even do a small do-something-funny Delphi application. Be sure to visit all the links in the above paragraph.

       Some exercises for you...
    Since this Course is an online course, there is much you can do to prepare for the next chapter. At the end of each chapter I'll try to provide several tasks for you to get more familiar with Delphi and the topics we discuss in the current chapter. Here are some exercises for you, after you finish reading this chapter:

    1. Play with the Color property of the Form object
    2. Use the Font property Editor to alter the font of the TLabel component
    3. Find out about the PasswordChar property of the TEdit component to create a simple password dialog form
    4. Try adding a code for the OnCreate event of a form to make a form appear centered on a screen. Also, make yourself familiar with the Position property of the TForm object.
    5. Be sure not to miss the next chapter!

       To the next chapter: A Beginner's Guide to Delphi Programming
    This is the end of the fourth chapter, in the fifth chapter, we'll explore the Code Editor window to help you in understanding the source behind the Form widow.

    If you need any kind of help at this point, please post to the Delphi Programming Forum where all the questions are answered and beginners are treated as experts.

    First page > Placing Components on a Form > Page 1, 2

    A Beginner's Guide to Delphi Programming: Chapter 5 >>
    >> Understanding the Delphi unit source

  • ©2017 About.com. All rights reserved.