tags:

views:

107

answers:

1

I'm trying to copy, or 'exchange' two forms that are referenced by a TListBox.

Here's what I'm trying to do, but I get an error (shown below):

      cf1 := TCustomform(lstPackages.Items.Objects[origNdx]);
      cf2 := TCustomform(lstPackages.Items.Objects[origNdx - 1]);

      cfTmp.Assign(cf1); //error here: cannot assign TfPackage to a TfPackage
      cf1.Assign(cf2);
      cf2.Assign(cfTmp);

      lstPackages.Items.Exchange(origNdx, origNdx - 1);
      lstPackages.ItemIndex := origNdx - 1;

So, I'm trying to exchange the list items, and I need to do something similar with the forms, but I get the error that I can't assign the form type that I'm using. TfPackage is a descendant of TCustomForm.

How can I accomplish the same thing?

+1  A: 

You don't have to do this. TStrings.Exchange exchanges the objects as well as the strings, so it's already taken care of for you. The same form objects will stay associated with the same strings.

EDIT: In response to the comment, if you need to exchange the position of the forms in another list, then that's not difficult. You've got the basic idea right when you said:

cfTmp.Assign(cf1);
cf1.Assign(cf2);
cf2.Assign(cfTmp);

But you're not trying to copy the objects, you're trying to swap references to them. Objects are not records. In Delphi, all object variables, including the ones in the form container, are references (hidden, implicit pointers) to the object. So what you need to do is:

cfTmp := list[cf1Position];
list[cf1Position] := list[cf2Position];
list[cf2Position] := cfTmp;
Mason Wheeler
I understand that. But these forms are also inside of a TFormContainer (from Billenium Effects), and I need to exchange their position in its internal list, too.
croceldon
@croceldon: OK. See my edit.
Mason Wheeler
The only internal list TFormContainer appears to have is the "LRU" list. It doesn't make sense to manually edit the order of that list; it's determined automatically based on when each form was last used. Rearranging your list box doesn't and shouldn't change the usage history of the forms. It sounds like you're using TFormContainer's list for something it's not designed for.
Rob Kennedy
I'm trying to swap references in the TFormContainer.Forms[] list, but I'm not sure that's possible....
croceldon
If you're trying and it takes you more than 30 seconds to find out, then you're doing it wrong. Use the three lines at the end of Mason's answer. If they compile, then you have your solution. If they don't compile, then the list is read-only; contact the component vendor for support. Or come up with another way that doesn't require the indices in the list box to be the same as the indices in the form container.
Rob Kennedy