Solved

Referencing TObjects

Posted on 2001-06-23
6
321 Views
Last Modified: 2010-04-06
There has to be a better way to do this...

I want to reference an Object depending on the procedure's sender. Sadly, I am still intimidated by pointers.


Here is an example of my current code (simplified)

// Bad way
procedure TForm1.ButtonClick(sender: TObject);
begin
 if sender = Button1 then
  begin
  Panel1.Visible := TRUE;
  Panel1.Top := 100;
  Panel1.Left := 100;
  end;
 if sender = Button2 then
  begin
  Panel2.Visible := TRUE;
  Panel2.Top := 100;
  Panel2.Left := 100;
  end;
 if sender = Button3 then
  begin
  Panel3.Visible := TRUE;
  Panel3.Top := 100;
  Panel3.Left := 100;
  end;

//etc....

end;




// better way ???

procedure TForm1.ButtonClick(sender: TObject);
 var // P: pointer?
begin
 P.Create;

 if sender = Button1 then P := Panel1;
 if sender = Button2 then P := Panel2;
 if sender = Button3 then P := Panel3;

 with P do
 Visible := TRUE;
 Top := 100;
 Left := 100;
 end;

 P.Free;
end;
 
1) Please help decaring the reference object and anything else that must be done to keep the code clean..
2) If I P.Free, will it also free Panel1?

Many Thanks.
Don

0
Comment
Question by:d32coder
  • 2
  • 2
  • 2
6 Comments
 
LVL 27

Accepted Solution

by:
kretzschmar earned 50 total points
ID: 6221213
// better way ???
maybe

                     procedure TForm1.ButtonClick(sender: TObject);
                     var  P: TPanel;
                     begin
                     //P.Create; no we have an existing one

                     if sender = Button1 then P := Panel1;
                     if sender = Button2 then P := Panel2;
                     if sender = Button3 then P := Panel3;

                     with P do
                     Visible := TRUE;
                     Top := 100;
                     Left := 100;
                     end;

                     //P.Free;  //no this would free the panel
                     end;
                     
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 6221484
You could also use the Tag property (if not already in use for something else) to store your reference:

        procedure TForm1.FormCreate(sender: TObject);
        begin
                Button1.Tag:=Longint(Panel1);
                Button2.Tag:=Longint(Panel2);
                Button3.Tag:=Longint(Panel3);
        end;

        procedure TForm1.ButtonClick(sender: TObject);
        begin
                with TPanel(TComponent(Sender).Tag) do begin
                        Visible := True;
                        Top := 100;
                        Left := 100;
                end;
        end;

Nice, eh? The only thing you have to do is to set the Tag property once for each TButton component as shown for this to work.
0
 

Author Comment

by:d32coder
ID: 6222924
I would NEVER have thought of doing that with tags.  How does it work? I thought a tag was an integer value??
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 27

Expert Comment

by:kretzschmar
ID: 6223407
its just a conversion

integer is a 4 Byte value
pointer is a 4 byte value

so you can store a pointer via conversion into a integer field/property

the conversion is shown by avonwyss
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 6224329
Thanks kretzschmar for the explanation. Since all information contained in a Pointer can be stored in an Integer, I just do the "conversion" (actually, it's only a typecast) back and forth when needed. This gives me compact and fast code then, avoiding any IF's after the initialisation.
0
 

Author Comment

by:d32coder
ID: 6225767
I used both examples and each solved a different problem.  
AvonWyss, I'll send you some points too.
Thanks to all.

Don
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Magic Software info 18 139
Convert GUI app into console app for Win32 Env 5 112
Multiple image collision 13 81
How to create virtual printer while installing my application 5 61
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question