Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Pass a variable from one form to another

Pass a variable from one form to another

Pass a variable from one form to another

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.


RE: Pass a variable from one form to another

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:


where FStringVariable is defined in the public section of the interface for Form1.

RE: Pass a variable from one form to another

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.


RE: Pass a variable from one form to another

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 "CustomerID" 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

   with TCustomerForm.Create(Application) do
       CustomerID := WhatTheUserWants;

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.


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close