?
Solved

Referencing TObjects

Posted on 2001-06-23
6
Medium Priority
?
328 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 200 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month16 days, 7 hours left to enroll

862 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