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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Data In different forms

Status
Not open for further replies.

rzrdigo

Programmer
Jan 7, 2011
19
BR
Hi,

i wanted to use in Form2 the data given in the Edits or calculated in Form1. How can i do this?

Thanks in advance.
 
Perhaps I misunderstand your question. You can #include the header file for Form1 in Form2:

Header for Form2:
Code:
#include "Form1.h"

Because all the VCL you place on a TForm, such as TEdit, etc... are published, including the Form1 header in Form2 will expose all of Form1's published and public data. Though quick and convenient, my problem with this approach is it is a real pain to use Form2 in other programs. So I can use my secondary forms in other programs, I pass data via the constructor, which seems to work nicely:

Header for Form2:
Code:
class TfrmLampSerNum : public TForm
{
__published:	// IDE-managed Components
	TGroupBox *gbLampSerNumbers;
	TLabeledEdit *lbledtLampSerNum_I;
	TLabeledEdit *lbledtLampSerNum_II;
//blah, blah, blah

private:	// User declarations
	void __fastcall EnablebtnOkay(void);
	void __fastcall HideLampSerNumbers(int iNumLamps);
	bool __fastcall GetNewLampSerNumData(void) const {return bNewLampSerNumData;}
	void __fastcall LookForDuplicateLampSerNumIDs(void);
	void __fastcall SizeForm(void);

	int iNumberOfLamps, iLampID;
	bool bNewLampSerNumData, bLampIDMatches[4], bFoundDuplicate;
public:		// User declarations
	__fastcall TfrmLampSerNum(TComponent* Owner, TmySQLDatabase *mySQLdbDesCenterMstr,
														int iNumLamps, int iSpecifiedLampID);
	TLampData *ldLampData[4];
	__property bool NewLampSerNumData = {read = GetNewLampSerNumData};
};
In the constructor "__fastcall TfrmLampSerNum", I not only pass a pointer to the main form (TComponent* Owner), I also pass data such as a pointer to the database connection, the number of lamps, and the primary key value of the lamp of interest. Form1 already has access to all of the published and public data of Form2, so I expose a TLampData helper class that I placed in Form2.

Steve.
 
In my form FMain, I use values given in the Edits to do some calculations on button click. I need to use the results of some of these calculations on another form. So should I do it like:

class Main : public TFMain
{
public:
Alfa1; //The result of a calculation done in FMain
}

 
I think I would use the OnActivate event of the second form to update the edit controls on it. That way, when you switch back to the second from from the main form, it will trigger an update. Alternately, you could put a button to trigger the update. Create your update function for the second form as a discrete member function first, then just plug it into the appropiate event handler fro there.
Be advised, if you are using the TEdit control supplied with the VCL, you will have to convert the values of the edits to numeric values, as the edits store only text values (AnsiString for pre-2009, UnicodeString for 2009, 2010 and XE).
 
Yes, then in Form2 you include the header file of Main as is shown in the first chunk of code above. To access your "Alfa1" from Main in Form2, you would use the following:

Somewhere in Main:
Code:
Alfa1 = Edit1->Text.ToDouble() + Edit2->Text.ToDouble();

Somewhere in Form2:
Code:
Alfa2_Form2 = Main->Alfa1 / 2.0;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top