Passing data from one form to another

  • Follow


I have a form that may create a second form. The first from has an array 
that I need to make available to the second form. This may also be the case 
for another string variable. How do I pass this data ? 

0
Reply franco.jommi (8) 11/15/2009 12:11:16 PM

Stark wrote:
> I have a form that may create a second form. The first from has an array 
> that I need to make available to the second form. This may also be the 
> case for another string variable. How do I pass this data ?

  When you create form, you can set the "Owner", with that, you can then
propagate back to the owner..
   The only exception to this rule is, you may need to know the class type.
  With that, you can insert in the uses list, the unit name that houses 
that form that creates the dynamic forms.


  At this point, you now can reference the Owner property of the newly
created form with in and do some back referencing.

   Using the "AS" and "IN" operators allows you to play with this.

  Example:

    Owner as TformX.MyArrayData[x] := .....

0
Reply Jamie 11/15/2009 3:12:02 PM


Let's see if I understood with the following example (while writing it, I 
realized I probably misunderstood..)

Unit Form1U
.........
.........
  Form2 := TForm2.Create(Self);
  try
    Form2.ShowModal;
  finally
    Form2.Free;
  end;
.........
.........
end;

----------------
Unit Form2U
  uses Form1U
 ...................
 procedure TForm2.FormShow(Sender: TObject);
 begin
   ...............
  firstElement:= Owner as TForm1.MyArrayData[0] ;
  ........

I think perhaps I should try ...

"Jamie" <jamie_ka1lpa_not_valid_after_ka1lpa_@charter.net> ha scritto nel 
messaggio news:fEULm.4319$JC2.3036@newsfe06.iad...
> Stark wrote:
>> I have a form that may create a second form. The first from has an array 
>> that I need to make available to the second form. This may also be the 
>> case for another string variable. How do I pass this data ?
>
>  When you create form, you can set the "Owner", with that, you can then
> propagate back to the owner..
>   The only exception to this rule is, you may need to know the class type.
>  With that, you can insert in the uses list, the unit name that houses 
> that form that creates the dynamic forms.
>
>
>  At this point, you now can reference the Owner property of the newly
> created form with in and do some back referencing.
>
>   Using the "AS" and "IN" operators allows you to play with this.
>
>  Example:
>
>    Owner as TformX.MyArrayData[x] := .....
> 

0
Reply Stark 11/15/2009 4:01:19 PM

See what I did for the string variable.
In Form2 (i:e. the form that is called):

  private
    { Private declarations }
    fDbName: string;
  public
    { Public declarations }
    property DbName: string read fDbName write fDbName;

In Form1 (i:e. the form that is calling):
  Form2 := TForm2.Create(Self);
  try
   Form2.DbName:= 'C:\Data\...';
    Form2.ShowModal;
  finally
    Form2.Free;
  end;

I got it somewhere on the net. Is this ok ? Of course , it's not ok for the 
array ...

"Stark" <franco.jommi@tin.it> ha scritto nel messaggio 
news:4affefd8$0$1415$4fafbaef@reader4.news.tin.it...
>I have a form that may create a second form. The first from has an array 
>that I need to make available to the second form. This may also be the case 
>for another string variable. How do I pass this data ? 

0
Reply Stark 11/15/2009 4:08:07 PM

Stark wrote:

> See what I did for the string variable.
> In Form2 (i:e. the form that is called):
> 
>  private
>    { Private declarations }
>    fDbName: string;
>  public
>    { Public declarations }
>    property DbName: string read fDbName write fDbName;
> 
> In Form1 (i:e. the form that is calling):
>  Form2 := TForm2.Create(Self);
>  try
>   Form2.DbName:= 'C:\Data\...';
>    Form2.ShowModal;
>  finally
>    Form2.Free;
>  end;
> 
That only modifies the form you just created. to get at the form
that created this one and assuming you're doing this inside of the
newly created form.

   Owner as Tform1.MyArray := ????
  or
  MyDataInSideMyNewForm := Owner as Tform1.MyArray.....
   or
   Tform1(Owner).MyArray..etc...

Something along those lines.
   You still need to have the unit that houses the Tform1
  listed in the USES of the unit that houses this TFORM2.



> I got it somewhere on the net. Is this ok ? Of course , it's not ok for 
> the array ...
> 
> "Stark" <franco.jommi@tin.it> ha scritto nel messaggio 
> news:4affefd8$0$1415$4fafbaef@reader4.news.tin.it...
> 
>> I have a form that may create a second form. The first from has an 
>> array that I need to make available to the second form. This may also 
>> be the case for another string variable. How do I pass this data ? 
> 
> 


-- 
"I'd rather have a bottle in front of me than a frontal lobotomy"

"Daily Thought:

  SOME PEOPLE ARE LIKE SLINKIES. NOT REALLY GOOD FOR ANYTHING BUT
  THEY BRING A SMILE TO YOUR FACE WHEN PUSHED DOWN THE STAIRS.
http://webpages.charter.net/jamie_5"

0
Reply Jamie 11/15/2009 4:47:22 PM

"Stark" <franco.jommi@tin.it> wrote in message 
news:4b00275b$0$1113$4fafbaef@reader2.news.tin.it...
> See what I did for the string variable.
> In Form2 (i:e. the form that is called):
>
>  private
>    { Private declarations }
>    fDbName: string;
>  public
>    { Public declarations }
>    property DbName: string read fDbName write fDbName;
>
> In Form1 (i:e. the form that is calling):
>  Form2 := TForm2.Create(Self);
>  try
>   Form2.DbName:= 'C:\Data\...';
>    Form2.ShowModal;
>  finally
>    Form2.Free;
>  end;
>
> I got it somewhere on the net. Is this ok ? Of course , it's not ok for 
> the array ...
>
> "Stark" <franco.jommi@tin.it> ha scritto nel messaggio 
> news:4affefd8$0$1415$4fafbaef@reader4.news.tin.it...
>>I have a form that may create a second form. The first from has an array 
>>that I need to make available to the second form. This may also be the 
>>case for another string variable. How do I pass this data ?
>


If you want the effect of pass-by-value, i.e. tForm2 only needs a copy of 
the data, then the code you wrote works.

You might consider something slightly tidier

Type
    tMyArray = array [{bounds}] of {type};

tForm2
    . . .
    public
        constructor CreateEx (aOwner : tComponent; const aString : string; 
anArray : tMyArray);
    . . .
    end;

constructor tForm2.CreateEx;

begin
Create (aOwner);
{store / use the passed data}
end;

The calling code in tForm1:

Form2 := tForm2.CreateEx (Self; myString; myArray);
try
    Form2.ShowModal;
finally
    Form2.Free;
    end; 

0
Reply BRoberts 11/16/2009 4:07:11 AM

Thanks, I'll try yous suggestion.

"BRoberts" <berdontemail@caneris.ca> ha scritto nel messaggio 
news:d2762$4b00cff3$45c49ff1$32090@TEKSAVVY.COM-Free...
> "Stark" <franco.jommi@tin.it> wrote in message 
> news:4b00275b$0$1113$4fafbaef@reader2.news.tin.it...
>> See what I did for the string variable.
>> In Form2 (i:e. the form that is called):
>>
>>  private
>>    { Private declarations }
>>    fDbName: string;
>>  public
>>    { Public declarations }
>>    property DbName: string read fDbName write fDbName;
>>
>> In Form1 (i:e. the form that is calling):
>>  Form2 := TForm2.Create(Self);
>>  try
>>   Form2.DbName:= 'C:\Data\...';
>>    Form2.ShowModal;
>>  finally
>>    Form2.Free;
>>  end;
>>
>> I got it somewhere on the net. Is this ok ? Of course , it's not ok for 
>> the array ...
>>
>> "Stark" <franco.jommi@tin.it> ha scritto nel messaggio 
>> news:4affefd8$0$1415$4fafbaef@reader4.news.tin.it...
>>>I have a form that may create a second form. The first from has an array 
>>>that I need to make available to the second form. This may also be the 
>>>case for another string variable. How do I pass this data ?
>>
>
>
> If you want the effect of pass-by-value, i.e. tForm2 only needs a copy of 
> the data, then the code you wrote works.
>
> You might consider something slightly tidier
>
> Type
>    tMyArray = array [{bounds}] of {type};
>
> tForm2
>    . . .
>    public
>        constructor CreateEx (aOwner : tComponent; const aString : string; 
> anArray : tMyArray);
>    . . .
>    end;
>
> constructor tForm2.CreateEx;
>
> begin
> Create (aOwner);
> {store / use the passed data}
> end;
>
> The calling code in tForm1:
>
> Form2 := tForm2.CreateEx (Self; myString; myArray);
> try
>    Form2.ShowModal;
> finally
>    Form2.Free;
>    end; 

0
Reply Stark 11/17/2009 1:30:28 PM

6 Replies
1396 Views

(page loaded in 0.106 seconds)

Similiar Articles:













7/23/2012 5:51:45 AM


Reply: