Passing Parameters trough Forms.

I have a MDI application that creates forms dynamically.  I want pass parameters from MDI Children Forms to other forms created by the MDI Children Forms uppon a mouse click.
jconde2Asked:
Who is Participating?
 
linda101698Connect With a Mentor Commented:
jconde2 wants to split the points between three of you.  I'm posting what looks to me to be the final solution so it can be saved.  I'll 150 points to his account which he can use to post questions for the three experts who helped come up with the final solution.  Thanks to all for the GREAT cooperation on this question.

Linda Gardner
Customer Service @ Experts Exchange

     this is the compete code:

     UNIT1 (This one "calls" the form passing parameters)

     unit Unit1;

     interface

     uses
       Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
     Dialogs,
       StdCtrls;

     type
       TForm1 = class(TForm)
         Button1: TButton;
         procedure Button1Click(Sender: TObject);
       private
         { Private declarations }
       public
         { Public declarations }
       end;

     var
       Form1: TForm1;

     implementation

     {$R *.DFM}

     uses Unit2;

     procedure TForm1.Button1Click(Sender: TObject);
     var
       NewForm: TForm2;
     begin
       NewForm := TForm2.Create(Application, 'Test message');
       NewForm.Show;
     end;

     end.


     UNIT2 (This one contains the Form "called" with parameters)

     unit Unit2;

     interface

     uses
       Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
     Dialogs;

     type
       TForm2 = class(TForm)
       private
         { Private declarations }
       public
         { Public declarations }
         Constructor Create(AOwner: TComponent; Parameter1: string); virtual;
       end;

     var
       Form2: TForm2;

     implementation

     {$R *.DFM}

     Constructor TForm2.Create(AOwner: TComponent; Parameter1: string);
     begin
       inherited Create(AOwner);
       ShowMessage(Parameter1);
     end;

     end.

     Notice the virtual declaration instead of override. (I shoud have tested before
     ;)))

     IHTH
0
 
Edo082297Commented:
Hi jconde2

  What exactly do you want to do?

Edo


0
 
jconde2Author Commented:
I created a MDI Child form with

var
  Child: TClass_Name;
begin
  Child := TClass_Name.Create(Application);

from my MDI parent from  (FORM1).  Once the child is created (FORM2), I created another form in the same way (FORM3), and this form needs to get a few parameters from the second form created.

In other words, FORM3 needs to receive some string values from FORM2 and I don´t know how can I accomplish this.

Thanx

jconde
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
jconde2Author Commented:
I created a MDI Child form with

var
  Child: TClass_Name;
begin
  Child := TClass_Name.Create(Application);

from my MDI parent from  (FORM1).  Once the child is created (FORM2), I created another form in the same way (FORM3), and this form needs to get a few parameters from the second form created.

In other words, FORM3 needs to receive some string values from FORM2 and I don´t know how can I accomplish this.

Thanx

jconde
0
 
jconde2Author Commented:
Adjusted points to 100
0
 
ZifNabCommented:
Hi jconde2, I did this myself once...

This isn't at all difficult you know.

Let presume the name of Form2 is Form2:-) and of Form3 is Form3

The parameters are passed after you've created your Form3 (Form2 already exists)

Form3 := ...Create...
From3.Parameter1 := Form2.Parameter1;
Form3.Parameter2 := Form2.What_Do_I_know;
..

Thats all you got to do...

You know your childs names, because they are kepped in a list.

 MDIChildren[I].....

Or you can use you're own list with TForms if this would make your life easier.... maybe every different childtype in another list... etc.

Regards, Zif.
0
 
jconde2Author Commented:
Thought you where quitting ZiF?.....

Anyways, Its great to have you arround here!

Zif, the problem I have is the following......
I need the parameters when the form is created!!

Can you help me out please?

I'll increase the points too!
Be cool
jconde
jconde
0
 
jconde2Author Commented:
Adjusted points to 101
0
 
ZifNabCommented:
Yes, I'm quiting, but just for a few weeks... need a vacation...

What is the really problem, because I'm missing here something.... Is the problem that the form isn't created yet or is it because you need to assing the parameters before creating?

Zif.
0
 
jconde2Author Commented:
That's good to know!  As someone stated, It's great to have "Real" experts arround!

this is the code I have:

var
  Design_Table: TDesign_Table;
begin
  Design_Table := TDesign_Table.Create(Application);
  Design_Table.Current_DB_S := Database.Current_DB;
  Design_Table.Port_S := Database.Port2;
  Design_Table.Host_S := Database.Server2;
  Design_Table.Password_S := Database.Password2;
  Design_Table.User_S := Database.User2;

Where Design_Table is the form being created and Database is the current form.

The problem is that in the Design_Table.Create method, the parameters I'm sending don't exist yet.  What method should I use instead of create, or what else can I do?

later

jorge
0
 
BlackManCommented:
How about storing the informations in the main form, then you'll always know where they are (not depending on how many MDIClients are created)...
0
 
viktornetCommented:
Can't you create the second and third form in the OnShow() of the First form???

Regards,
Viktor Ivanov
0
 
ZifNabCommented:
Hi jorge,

don't make my neck bigger then it already is :-). Thanks.

On to your problem ... I still don't have the slightest idea what you really want (Sorry).

As I see in your code, every parameter is put in the new created form like you wanted... or not??

TDesigntable looks like this :

TDesigntable = object ....
 ...
 public
  Current_DB_S ...
  Port_S ...
  Host_S ...
  Password_S ...
  User_S ...
 end;

Now when you create the form, they are empty.
After you created them, you fill them up (like in your code)
Then you call a procedure to set all things in place?
Then you call show...

Correct? Missing here something?

Zif.

 
0
 
jconde2Author Commented:
What do you mean by:
Then you call a procedure to set all things in place?

I tried a messagedlg with one of the parameters I sent in the onShow method and I get an access violation error.

jorge
0
 
ZifNabCommented:
Well, I don't know if you need this procedure, but you can call this procedure so you everything will be done what has to be done. e.g. (very dumb) pass a name to certain parameter, then call the procedure search_name. And then show form. Search_name will search for a name in a database and place all information onto form and when form becomes visible the correct data is shown. This is offcourse a very dumb example, because you also could do this by :

property name: String read fName write SearchName(vName:String);

but if you've to set several parameters, maybe it's better not doing it like above, but using a procedure which will do everything at once after all parameters are set.

Zif.
0
 
jconde2Author Commented:
Thank you zif, go ahead and post your answer!

jorge
0
 
ZifNabCommented:
? jconde2 ? Was this what you were looking for? Euhm? What have I said so it solved your solution? Zif.
0
 
ZifNabCommented:
... last sentence has to be : What have I said to solve your question? :-) Zif.
0
 
jconde2Author Commented:
Thank you zif, go ahead and post your answer!

jorge
0
 
jconde2Author Commented:
I got an Idea from the sentence:

property name: String read fName write SearchName(vName:String);

I'll create a custom form object that accepts the parameters before create!

later

jorge
0
 
itamarCommented:
Hi jconde2,

I´m a little late in this question, but I think that the cleaner solution is to have a descendant form with a overrided Create (constructor) procedure.
Smth like this:

TMyForm: class(TForm)
.
Constructor Create(AOwner: TComponent; Parameter1, ParameterN: string); override;

.
implementation

Constructor Create(AOwner: TComponent; Parameter1, ParameterN: string);
begin
  inherited Create(AOwner);
  //Do smth with Parameters...
end;

IHTH
0
 
ZifNabCommented:
mmm, Itamar looks like a good idea. But what if you don't want always react to these parameters. Anyway, good idea. Zif.
0
 
viktornetCommented:
Actually I there  are no parameters by default in the Create procedure of a form...you need to create a seperate procedure works with the parameters and then call it in the Create();override;
0
 
ZifNabCommented:
Viktornet, well yes, didn't thought on that (that create doesn't has a default one). But then using a procedure which creates the form and calls create itself... then I like my solution better. Zif.
0
 
viktornetCommented:
Here is an example.......
type
  TMyForm = class(TForm)
  constructor Create(AOwner : TComponent);override;
  procedure WorkWithParams(Param1 : Integer; Param2 : String);virtual;
   //Virtual so you can override it again if you need to without creating a
   //new class all over again
end;

Constructor TMyForm.Create(AOwner : TComponent);
begin
  Inherited Create(AOwner)
  WorkWithParams({The params});
end;

procedure TMyForm.WorkWithPrams({The params});
begin
  //Do what you want with them
end;

Regards,
Viktor Ivanov
0
 
viktornetCommented:
I think Zif's idea is the easiest one to implement and work with ;)

Regards,
Viktor Ivanov
0
 
jconde2Author Commented:
Thanks viktor!


0
 
jconde2Author Commented:
Well, who's going to post an answer??????????????????

Thanks to all of you!   Once again, my project will be on time!

Regards,
Jorge
0
 
viktornetCommented:
No prob :-)

Regards,
Viktor Ivanov
0
 
ZifNabCommented:
well jorge, it's up to you... it all depends on what you need to program. Does it stays fixed (I mean do you need always to pass the same parameters) or do you sometimes only need to pass some parameters and then react just on these...
I guess you've enough examples now.
Regards, Zif.
0
 
viktornetCommented:
Guys, why don't you send an answer??? You got the answers right ;)

Regards,
Viktor Ivanov
0
 
itamarCommented:
Hi all,

this is the compete code:

UNIT1 (This one "calls" the form passing parameters)

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

uses Unit2;

procedure TForm1.Button1Click(Sender: TObject);
var
  NewForm: TForm2;
begin
  NewForm := TForm2.Create(Application, 'Test message');
  NewForm.Show;
end;

end.


UNIT2 (This one contains the Form "called" with parameters)

unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
  TForm2 = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
    Constructor Create(AOwner: TComponent; Parameter1: string); virtual;
  end;

var
  Form2: TForm2;

implementation

{$R *.DFM}

Constructor TForm2.Create(AOwner: TComponent; Parameter1: string);
begin
  inherited Create(AOwner);
  ShowMessage(Parameter1);
end;

end.

Notice the virtual declaration instead of override. (I shoud have tested before ;)))

IHTH
0
 
jconde2Author Commented:
Go ahead vik, zif.....you're the two that helped me the most!

Shame I can't split points!

jorge
0
 
jconde2Author Commented:
Go ahead vik, zif.....you're the two that helped me the most!

Shame I can't split points!

jorge
0
 
itamarCommented:
I think jconde2 should say smth before we submit an answer, right ?
0
 
viktornetCommented:
Well Zif??? Wanna get 'em?

//Vik
0
 
ZifNabCommented:
yup, agree, but I'm up to my bed, have to get up at 6 and oh my look at the time already... I'll hear from you tomorrow or should I say later on the day?
0
 
jconde2Author Commented:
Guess so!

Good night and thank you zif

jorge
0
 
itamarCommented:
Hi all,

I don´t see the difficulty in my solution, but go ahead...
0
 
ZifNabCommented:
euhm, agree was meant on : I think jconde2 should say smth before we submit an answer, right ?

Zif.
0
 
jconde2Author Commented:
Guess so!

Good night and thank you zif

jorge
0
 
viktornetCommented:
OK, Bye all....sleep tight,....This conversation becam as long as  novel ;-)

Regards,
Viktor Ivanov
0
 
viktornetCommented:
Zif answer it so it gets closed ;-)
0
 
jconde2Author Commented:
Since Zif, Vik and Itamar helped me a lot, please do help me out again!

In your personal opinnion, who deserves the points???

Your comments will be very appreciated!

P.S.  (Itamar and Vik gave me Source examples!)

jorge
0
 
viktornetCommented:
ZIF
0
 
viktornetCommented:
Haven't you decided yet??? Zif is almost sleeping ;) Right zif?

Regards,
Viktor Ivanov
0
 
viktornetCommented:
Hey, guys I'm getting outta here, and you decide who's gonna get the points ;) I think this is fair, right?

Regards,
Viktor Ivanov
0
 
jconde2Author Commented:
Viktornet, Itamar, Zifnab:

Roll the dice and take your shot.
The 3 of you helped me out alot, and if I could, I'd split the points in 3.  

Since that's not the case, shoot!!!!!!!!!

The first one in will get an A.

thanx again!

jorge
0
 
itamarCommented:

I myself ;) Itamar (sincerely, cause your question talk about passing parameters when creating forms AND I think this is the best OO approach for the problem. I´m not caring with the points, I just would like to show my point-of-view)
0
 
itamarCommented:
Hey !

E-E administration people can share the points. I did it myself sometime.
0
 
itamarCommented:
Post a last comment authorizing E-E people to do so and I´ll take care of it.

It´s early in my place...
0
 
itamarCommented:
Post a last comment authorizing E-E people to do so and I´ll take care of it.

It´s early in my place...
0
 
jconde2Author Commented:
Fine with me itamar!
Let's do it!

jorge
0
 
viktornetCommented:
Delete the question and send three new questions with the same number of points for three of us...until you see each of us answered one any of them you accept the answers ...

//vik
0
 
jconde2Author Commented:
It might sound stupid, but how do I delete the question???

all the options I get are
 increase points
 comment to add
 email notification

jorge

0
 
viktornetCommented:
If someone has answered already the Q and you rejeted than that wouldn;t be possible...I don't see the histpry right now so I can't see the rest of the page....
0
 
jconde2Author Commented:
Please split the points in 3 and grade zifnab, itamar and viktornet with an A respectively.

thanx
jconde2
0
 
jconde2Author Commented:
Im out!!!

See you all tomorrow!

jorge
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.