Solved

Referencing TObjects

Posted on 2001-06-23
6
316 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now