| Learn about: properties, events and Delphi Pascal | |||||||||||||||||||
| Page 2: Changing Component Properties; Writing Code - Events and Event Handlers | |||||||||||||||||||
Changing Component Properties 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. ![]() 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 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.
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.
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.
We'll now write some code for the OnClick event handler of Button1. Alter the above procedure body to:
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... ![]() What follows is a simple explanation of the code that runs this application. Let's see.
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...
1. Play with the Color property of the Form object To the next chapter: A Beginner's Guide to Delphi Programming 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 >> |
|||||||||||||||||||


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.
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 
