Responding to Events


To execute a piece of code in a VCL Forms application, you have to write a response to an event. The first application we'll create displays the user-entered text on the form's title bar.

To accept user input in VCL Forms applications, you cannot use the ReadLn procedure. Instead, you have to use the TEdit component. The Text property of the TEdit component enables you to read and write text displayed in the text box.

First, place a TEdit component on the form. This results in the component displaying its name on the form. Before we continue, remove the text from the TEdit component by clearing the Text property in the Object Inspector.

To copy the text from the TEdit component to the title bar, we have to write a response to an event. Without using a button, we can copy text from the TEdit component to the title bar in response to the OnChange event. The OnChange event occurs every time the user changes the contents of the text box.

To write a response to the OnChange event, select the Edit1 component on the Designer Surface and display the component's list of events in the Object Inspector. The OnChange event is the first event in the list.

image from book
Figure 11-13: The OnChange event

Before you can write code, you have to create an empty event handler for the OnChange event. To create an empty event handler, double-click the right column in the Events page. Delphi creates an empty event handler in the Code Editor and adds the name of the event handler to the right column of the Events page (see Figure 11-14). The name of the event handler is always derived from the component and event names.

image from book
Figure 11-14: An empty event handler

As you can see, an event handler is nothing more than a procedure. Every time the user changes the contents of the Edit component, the Edit1Change procedure is called. To copy the text from the Edit1 component, you only have to write a single line of code:

procedure TForm1.Edit1Change(Sender: TObject); begin   Caption := Edit1.Text; end;

The result of the OnChange event handler is displayed in Figure 11-15.

image from book
Figure 11-15: The OnChange event handler

Responding to the OnClick Event

The most frequently used event is the OnClick event. The following example enables the user to reverse a string. The example uses two TEdit components for input and output and a TButton component that reverses the string in the OnClick event handler (see Figure 11-16).

image from book
Figure 11-16: The user interface of the reverse string application

Again, to provide the user with a more professional and nicer-looking user interface, you should delete the automatically generated text in the TEdit components. To be more productive, you should first select both TEdit components and then clear their Text properties in the Object Inspector. To select more components on the Designer Surface, first select a single component, then press and hold the Shift key and select other components.

When you select more components, the Object Inspector shows how many components you have selected and only displays the properties that exist in all selected components (see Figure 11-17).

image from book
Figure 11-17: Multiple component selection

To write code for the OnClick event, you have to generate the empty event handler. Currently, there are two ways to achieve this because the OnClick event is the default event of the TButton component. The standard way of creating an event handler is to display the Events page in the Object Inspector and double-click the right column next to the event name. This is how you create empty event handlers for all events. But, if you want to create the event handler for the default event, you can simply double-click the component on the Designer Surface. After you double-click the Button1 component on the Designer Surface, Delphi creates the following event handler:

procedure TForm1.Button1Click(Sender: TObject); begin end;

To easiest way to reverse the string is to use a for-downto loop and copy characters from one TEdit component to the other. Note that this isn't the fastest way of reversing a string.

Listing 11-2: Reversing a string

image from book
procedure TForm1.Button1Click(Sender: TObject); var   i: Integer; begin   Edit2.Text := ''; { clear the old text }   for i := Length(Edit1.Text) downto 1 do     Edit2.Text := Edit2.Text + Edit1.Text[i]; end;
image from book

Removing Event Handlers

Delphi's Code Editor has always been far superior to editors in other development environments, especially when it comes to code management. For instance, the Code Editor has the ability to automatically remove empty event handlers from the source code. If you, either deliberately or accidentally, double-click the Designer Surface, the Code Editor generates an empty event handler for the form's OnCreate event:

procedure TForm1.FormCreate(Sender: TObject); begin end;

If you don't have to respond to the OnCreate event, you will probably want to remove this unwanted piece of code. Although you can, you shouldn't remove the event handler manually because Delphi automatically removes all empty event handlers when you save the file or project to disk.

Removing an event handler that contains code involves some work. For instance, let's remove the OnClick handler of the reverse string button. Again, you should refrain from removing the method implementation manually. If you do, you will break your code and will be unable to compile the application (see Figure 11-19).

image from book
Figure 11-18: The reverse string application

image from book
Figure 11-19: Don't remove method implementations manually.

If you've removed the method implementation manually, you then have to delete the method header in the interface section of the unit. The fastest way to delete a single line of code is to place the cursor anywhere in the line you want to delete and press Ctrl+Y.

The best way to remove an event handler from the source code is to only remove the code you added to it. In the case of the reverse string button, you have to remove the declaration of the variable and the code in the method block:

procedure TForm1.Button1Click(Sender: TObject); begin end;

When you're left with an empty event handler, you can remove the handler and all references to it by saving the project to disk.

The ability to remove empty event handlers is great and usually extremely useful. The only time this functionality can cause trouble is when we want to have empty event handlers as placeholders for future code. To trick the Code Editor into not removing the empty event handlers, write an empty comment in the method block:

procedure TForm1.Button1Click(Sender: TObject); begin   { } end;



Inside Delphi 2006
Inside Delphi 2006 (Wordware Delphi Developers Library)
ISBN: 1598220039
EAN: 2147483647
Year: 2004
Pages: 212
Authors: Ivan Hladni

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