Click to See Complete Forum and Search --> : calling a textBox from a 2nd Form


rowgram
November 22nd, 2005, 06:47 PM
I've got Form2 which is created from Form1 :
(inside a button click event handler) :

namespace::Form2 *myForm2 = new namespace::Form2;
myForm2->Show();


This Form2 works well, & I now have the need to write text to it's textBox1.
So I create one in Form2[Design]. I need to do this writing inside a function X that is defined within namespace but outside the definition of Form2. Function X is called from another button click event handler of Form2.
How should I do this ?
I've tried :
(inside the function X definition) :

namespace::Form2::textBox1->set_Text(blah blah blah);

but I get error C2227 : left of '->' must point to class/struct/union

I can't seem to shake this error - any advice ? Why is textBox1 is not seen as a class ? Under Class View - I see it displayed just as other textBoxs of Form1.

ak

Alex F
November 23rd, 2005, 04:32 AM
Try this:

myForm2->textBox1->set_Text(blah blah blah);

rowgram
November 23rd, 2005, 08:42 AM
Yes , I've tried this, but I in addition to error C2227, I get error C2065 : 'myForm2' : undeclared identifier.

However, when I put the myForm2->textBox1->set_Text(...); statement right under the myForm2 instantation (inside the Form1.h definition, right under the myForm2->Show(); statement), there is no error. Unfortunately that's not where I want to put the set_Text statement...

ak

Alex F
November 23rd, 2005, 09:08 AM
If function X is called from another button click event handler of Form2, you can pass Form2* pointer to it as parameter:


void X(Form2* pForm2)
{
pForm2->textBox1->set_Text(...);
}



Button click event handler of Form2:

SomePointer->X(this);

rowgram
November 23rd, 2005, 09:21 AM
can you elaborate on the 2nd part ?
I've added Form2* pForm2 to the parameters of X (although it currently tells me that Form2 is an undeclared identifier).

What does SomePointer->X(this); mean ? Would this be placed immediately before the call to function X ?

Alex F
November 23rd, 2005, 10:06 AM
Please show your code, with namespace and class definitions.

rowgram
November 23rd, 2005, 10:54 AM
Unfortunately I can't, but I have attached a stripped down version. Perhaps there is something wrong in this ?

Note some of the comments in the header files.

Form1-out is really called Form1.h, same with Form2-out.h .

Any help would be appreciated.

Thanks,
ak

Alex F
November 23rd, 2005, 11:13 AM
namespace tester
{
BOOL X(Form2* pForm2, ...)
{
pForm2->textBox1->set_Text("hi");
}
}

...

private: System::Void button2_Click(System::Object * sender, System::EventArgs * e)
{
dStatus = tester::X(this, var1, var2);
}

rowgram
November 23rd, 2005, 11:42 AM
It tells me that Form2 is undeclared identifier - this error occurs in the function prototype statement inside general.h

rowgram
November 23rd, 2005, 03:01 PM
Any idea why Form2 is not seen as a valid class ?

ak

rowgram
November 29th, 2005, 01:07 PM
I think I'm close :

I have defined textbox1 in the Form2 class as a public, rather than private, member.

The function X looks like this : (notice the Form2* pForm2 has been removed) :

BOOL X ( z, //other arguments//)
{
Form2* pForm2;
//more code
pForm2->textBox1->set_Text(z);
//more code
}

Everything compiles :), but when it gets to the pForm2 statement I get an unhandled exception :
object reference not set to an instance of an object.

Way back inside Form1, Form2 was defined as :
Form2 *myForm2 = new Form2;
myForm2->Show();

How can I resolve this ?
Is there a way to call myForm2 inside X ? Should this be done ?

Any feedback would be appreciated -
ak