Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pointer to a TForm

Status
Not open for further replies.

LucieLastic

Programmer
May 9, 2001
1,694
GB
hi All

How do you add a TForm to a TList as a pointer then access it later eg:-

FormCaption := TForm(MyList[idx]).Caption;

or

TForm(MyList[idx]).Free;

I was trying to add the forms to the list like this:-
MyList.add(@NewForm) - is this right?

Also, I want to pass the pointer to other methods and having trouble with type mismatches.

I tried a method declaration like this but the Indexof fails and it's definitely in the list - think the 'pointer' bit in declaration is wrong.

procedure Tmain.RemoveFormFromList(FrmPtr: pointer);
var idx : integer;
begin
idx := WindowList.IndexOf(FrmPtr); <---This is always -1
if idx > -1 then
WindowList.Delete(idx);
end;


I seem to get one bit right then another bit fails. All I want to do is have a TList of open forms and later find these forms in my list (using IndexOf) and delete them from the list aswell as Free them using the Form's pointer.

ta
lou

p.s. I was using the api call DestroyWindow but although this worked, it didn't call the destructors of my forms.
 
Sorted it (I think). I'm now adding the forms as TForm and passing them around like this aswell, rather than sending the address of a TForm.

Is this the best way though?
 
I can tell you exactly how to do it for records:
Code:
type
    TLeague = record
        TeamName: string;
        blah blah blah
    end;
    PLeague = ^TLeague;

later:
var LeagueList: TList;
    leagptr: PLeague;

later:
    New(leagptr);
    LeagueList.Add(leagptr);

later:
    if PLeague(LeagueList[i])^.TeamName <> '' then
        blah blah blah
So I declare my data structure, and declare a pointer type for it. Later I create records of that typ[e, and add pointers to them to the TList. Later again, when I reference elements of the TList, I have to cast them back with the PLeague(blah)^.

Of course, I don't know how this can be converted to work for objects. I think objects are referred to by pointer, so I would have thought the code you showed above was right.

Bear in mind that TComponent contains an array called Components that is specifically designed to hold other components; maybe use a TComponent instead of a TList? Not too sure about this, though. -- Doug Burbidge mailto:dougburbidge@yahoo.com
 
When you declare object variables you are actually using pointers anyway, so what you've done is correct.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top