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!

Pass a variable from one form to another 1

Status
Not open for further replies.

vincentw56

IS-IT--Management
Oct 10, 2002
47
US
I am still learning Delphi and getting away from VB. How do you pass the contents of one from variable to another? Right now I am using a datamodule to do it, but wanted to see if there is a better way. Thanks.

Vincent
 
There are so many ways...

Can you be a little more specific about what you want to do? Are you using .ShowModal? Are you creating the form yourself or letting Delphi do it? Do you have the first form referenced in a uses statement within the second form's unit?

One of the easiest ways is simply to qualify with the object name, e.g. in Form2:
Code:
   ShowMessage(Form1.FStringVariable);
where
Code:
 FStringVariable
is defined in the public section of the interface for Form1.
 
Actually what you showed me is exactly what I wanted. That is pretty much the same with VB except I did not make it public on Form1. Thanks for the help once again.

Vincent
 
Another way to do it is to treat the form as a sub-class of TForm, which it is! <s>

Instead of exposing fields, you can create a public or published property of a form class that can use accessors and mutators to set the caption, change the color or position, etc. Even though these new properties do not appear in the Object Inspector (at least not easily), they are still part of the class.

In fact, creating properties of forms makes them encapsulated, robust, and even polymorphic!. For Instance, you can have a form that displays customer information. It might have a property named &quot;CustomerID&quot; with a protected, virtual mustator (e.g. SetCustomerID). This mutator finds the correct row in the DataSet, not the Menu.OnClick or anything else. Any piece of code can just call
Code:
   with TCustomerForm.Create(Application) do
   begin
       CustomerID := WhatTheUserWants;
       Show;
   end;
[\code]

Any change to the CUSTOMER table (or tables) is encapsulated inside this form.

You can create a descendent of that form that allows specific editing of customer information. That descendent will override SetCustomerID and do the extra things it needs to for editing.

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top