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

How do I pass values/variables from a called form to a calling form? 2

Status
Not open for further replies.

tunday

Programmer
Jun 11, 2007
4
GB
I am relatively new to Borland 2006 C++ Builder. I have a form which has about 6 buttons and a text box for selecting and inputting values respectively (e.g. Clicking on Button 1 should return "100" to a variable in the calling form. Likewise, entering 100 into the text box and pressing enter would do same. This form is called by another form which needs to make use of the value selected. I have been able to return values from the text box. However, I need help on how to return values as a result of clicking the buttons.
Thanks in advance.


 
There might be more fancy methods but i solve such problems by making an event on the button, that event then sees to it that the valie is set in the correct way.

If you have the text-box as well uou could simply print the value in the box and return with that.

Totte
Keep making it perfect and it will end up broken.
 
Let's see if I can explain this without mudding everything up. Normally, when you create a form, Builder creates the header and source with the form info set up. For example, suppose you create a form with the name of MyNewForm. Builder creates in the header:
Code:
public:		// User declarations
    __fastcall TMyNewForm(TComponent* Owner);

while in the source code it creates
Code:
//---------------------------------------------------------------------------
__fastcall TMyNewForm::TMyNewForm(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------

Of course, more lines than these are created but these are the ones you need to know about for this discussion.

Now suppose you want to send an integer and an ansistring to the new form from the calling form. First, change the header by adding a new form declaration that uses the integer and string.
Code:
public:		// User declarations
    __fastcall TMyNewForm(TComponent* Owner);
    __fastcall TMyNewForm(int ANumberInt, AnsiString AStringStr, TComponent* Owner); // I added this line

In the source code, you will need to add:
Code:
__fastcall TMyNewForm::TMyNewForm(int ANumberInt, AnsiString AStringStr, TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------

So far so good. Now you need to get those new variables into your code. I do this by declaring a couple of global variables in the private section of the class in the header. This is found just above the public section you just modifed. I would put something like:
Code:
private:	// User declarations
    AnsiString MAStringStr; // added
    int MANumberInt; // added
public:		// User declarations

Then I modify the source code I just added to set them up.
Code:
__fastcall TMyNewForm::TMyNewForm(int ANumberInt, AnsiString AStringStr, TComponent* Owner)
    : TForm(Owner)
{
    MANumberInt = ANumberInt;
    MAStringStr = AStringStr;
}
//---------------------------------------------------------------------------

Finally, in the calling form you need to call the new form with the integer and string. Something like:
Code:
int DiffInt = 0;
AnsiString DiffStr = "New Diff";

// Create and destroy new form
try
{
    MyNewForm = new TMyNewFor(DiffInt, DiffStr, this);
    MyNewForm->ShowModal();
}
__finally
{
    delete MyNewForm;
    MyNewForm = 0;
}

All this assumes you are creating modal forms.

You can also send data back to the calling form by using pointers and assigning the pointer on the new form's closing method.

Clear as mud?



James P. Cottingham
-----------------------------------------
I'm number 1,229!
I'm number 1,229!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top