Forms in VC++
Started by
megha
, Mar 07 2006 04:15 PM
#1
Posted 07 March 2006 - 04:15 PM
#2
Posted 25 March 2006 - 01:39 PM
you can declare a public static pointer (to itself) in the first so that it can be accessed from the 2nd form & any other form for that matter. Or you could pass the data you require to the 2nd form when you instantiate it through its constructor.
Corrections:
static pointer I think I used in C# actually so if you're using C++ then just have a member pointer to a Form in the 2nd class & make an overloaded constructor to accept a Form pointer. Then when instantiating the 2nd form pass it the 'this' pointer from the 1st form (as long as you're instantiating it from the 1st form) and assign the passed in pointer to the already created Form pointer member in the 2nd Form. Then you can use the passed in pointer to access all of Form1's members.
Form1:
button_click(---------)
{
Form2 *newForm = new Form2(this);
newForm->Show();
}
class Form2:
{
Form *mForm1;
public Form2()
{
//default constructor
}
public Form2(Form *a1stForm) //new constructor
{
mForm1 = a1stForm;
}
public Form2::AnyMethod()
{
mForm1->Text = "Accessed from Form2";
}
Cheers,
Steve
Corrections:
static pointer I think I used in C# actually so if you're using C++ then just have a member pointer to a Form in the 2nd class & make an overloaded constructor to accept a Form pointer. Then when instantiating the 2nd form pass it the 'this' pointer from the 1st form (as long as you're instantiating it from the 1st form) and assign the passed in pointer to the already created Form pointer member in the 2nd Form. Then you can use the passed in pointer to access all of Form1's members.
Form1:
button_click(---------)
{
Form2 *newForm = new Form2(this);
newForm->Show();
}
class Form2:
{
Form *mForm1;
public Form2()
{
//default constructor
}
public Form2(Form *a1stForm) //new constructor
{
mForm1 = a1stForm;
}
public Form2::AnyMethod()
{
mForm1->Text = "Accessed from Form2";
}
Cheers,
Steve
Edited by StephenL, 25 March 2006 - 02:32 PM.
Similar Topics
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users