Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Pascal
Expertise: Beginner
Mar 19, 1997

How can I centralize the event handling for related controls?

Question:
I'm a VB programmer who is new to Delphi. How do I centralize event handling for related controls?

In VB, there is a concept of control arrays in which each control in the array is of the same type and has an index (subscript) that uniquely identifies it. This index is then passed as a parameter to all event subroutines that the particular control supports.

For example, below is an example of a three-control array event subroutine for three buttons:


     Private Sub Btn_Click(Index As Integer)

        Select Case Index

            Case 0            { Button 0 clicked }

                    MsgBox ("Yes")

            Case 1            { Button 1 clicked }

                    MsgBox ("No")

            Case 2            { Button 2 clicked }

                    MsgBox ("Maybe")

        End Select

     End Sub

Is there an equivalent in Delphi?

Answer:
Yes, there is an equivalent. In fact, Delphi's approach takes it a step further by allowing you to centralize events for different control types, as long as they can receive the same event.

Delphi does not have the concept of a control array, but instead has more logical implementation. Event handlers, as they are called in Delphi, are callback functions that are not tied to any particular control, but are assigned to a control's event property value.

What is a control event property value?

The Object Inspector, which is similar to the Properties window in VB, has two tabs: a Properties tab that looks and operates like the Properties window in VB, and the Events tab, which has all the events a particular control supports. Adjacent to each event is a combo box that stores the event property value. This is where you can manually add the name of an event handler or select from the drop-down list of already existing event handlers.

Think of the Events tab as a list of open jacks that you plug into if you are interested in performing a customized reaction to a specific event. In technical terms, these open jacks can potentially store the address of an event handler/callback function that will be called if that particular control experiences that event.

Below is an example of a Delphi event handler. What makes it share-able from the end-user perspective is the Sender parameter, which is of type TObject. All classes in Delphi are ultimately derived from TObject; therefore, you can always test for the specific object/control for which the event was generated.


procedure TForm1.Clicked(Sender: TObject);

begin

     if Sender = Label1 then

        ShowMessage ('Label clicked');



     if Sender = Button1 then

        ShowMessage ('Button clicked');



     if Sender = Edit1 then

        ShowMessage ('Edit clicked');

end;

DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date