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!

How to pass data from invokated form to main form 1

Status
Not open for further replies.

atreides009

Technical User
Apr 25, 2011
2
PL
Hello! I'm using Borland C++Builder (TurboC++ 2006). My problem is:

In my program I call a second form where the user can introduce a number in an Edit. Then, on the main form, I do some operations with that number. How can I pass the value from the second form to the main one?

I can't do what I read on the post thread101-1376709 (I didn't write there because it's closed) because I don't know where I have to write the last snippet code. The part where my project has those keywords (try, catch, throw, etc...) is different and I don't understand what it does. So, any more simple way to do that?
 
About the simplest way to access data in a TEdit assigned to a secondary TForm is to #include the header file for the secondary TForm somewhere in the code of the main TForm's unit code. You can then access the TEdit control 'directly'.

Imagine two forms; frmMainForm (found in MainFormUnit.cpp(h)) and frmSecondaryForm (found in frmSecondaryFormUnit.cpp(h)). Further imagine a TEdit control called edtNumericInput. Close to the top of the frmMainForm's code #include the header for the secondary form:

Close to the top of frmMainFormUnit.cpp
Code:
#include "MainFormUnit.h"
#include "frmSecondaryFormUnit.h"
I would recommend placing the header file for the secondary form in the header of the main form, but that is just me.

Then, just access the data when you are ready.

frmMainFormUnit.cpp
Code:
AnsiString asNumericDataFromSecondaryForm;

if (frmSecondaryForm)
  delete frmSecondaryForm;
frmSecondaryForm = new TfrmSecondaryForm(Application);
frmSecondaryForm->ShowModal();
asNumericDataFromSecondaryForm = frmSecondaryForm->edtNumericInput->Text;
delete frmSecondaryForm;
frmSecondaryForm = NULL;


I now, personally, *start* with 2ffat's suggestion by passing data (pointers, numbers, strings, etc...) to secondary forms via constructors JUST like 2ffat suggested. I then *finish* by using __property to return result data to the main form. I used to use the approach as shown above and the code is VERY UGLY and VERY difficult to maintain.

JUST as 2ffat suggested I overload my secondary form constructors so I do not have to rewrite old programs just because I changed the interface to my secondary forms. I reuse my forms as much as possible. I have a generic lamp serial number form a generic test equipment asset number form a generic product serialization update form, etc...


Steve.
 
Thank you so much for your help ;) The first option is enough for my code, I think, but anyway I'll go over the other way, too. It's more elegant and efficient. I'm still a beginner and I got to learn everything about function overloading and all the new features of C++. Thank you again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top