Click to See Complete Forum and Search --> : trouble with calling a Form


rowgram
December 3rd, 2005, 03:40 PM
I have a seperate function that is called inside a Windows Form. Both function & Form belong to the same namespace.
Inside this function, I need to display data of this function through textboxes. So I do :

namespace::Form2::textBox1->set_Text("hi");

I get error C2227 : left of ->set_Text must point to a class/struct/union.

What's going on & how can I fix this ?

kkez
December 4th, 2005, 07:20 AM
Is "textBox1" a pointer? if not, use . instead of -> to access class members

NoHero
December 4th, 2005, 10:26 AM
[ Moved Thread ]

NoHero
December 4th, 2005, 10:27 AM
namespace::Form2::textBox1->set_Text(S"hi");

You forgot the S before your string literal, to tell your compiler to treat is as System::String *.

rowgram
December 6th, 2005, 11:43 AM
Your right about the corrections, but the error is still there.
The trouble is left of the set_text function.

When I use textBox1 inside the Form2 class definition(which is NOT where I need it), the command :

textBox1->set_Text(S"hi")l;

works fine.

But when I place this command outside the Form2 definition, inside a seperate function, I get C2227 (left of ->set_Text must point to a class).

How am I supposed to use a Windows textbox outside of the definition of it's form ?

cilu
December 6th, 2005, 02:03 PM
You can't access it outside the class. Not unless you you pass a pointer to it:

void StandAloneFunction(System::Windows::Forms::TextBox* box)
{
box->set_Text(S"hello");
}

void FooForm::SomeMethod()
{
StandAloneFunction(textBox1);
}

rowgram
December 7th, 2005, 09:57 AM
Yes ! That does it ! Thank you for the help !

My final step is to set_text for a group of text boxes (~20), not just one. it would be cumbersome to increase the function prototype to handle this, so I'm trying to create a struct of pointers System::Windows::Forms::TextBox* box :

typedef struct
{
System::Windows::Forms::TextBox* tb1;
System::Windows::Forms::TextBox* tb2;
......
} textboxes;

& then define : StandAloneFunction(textboxes boxer)

but I'm not sure how to call this function with textBox1, textBox2, etc..


What would be the best way to pass a set of textbox pointers to the StandAloneFunction ?

NoHero
December 7th, 2005, 11:31 AM
Why not passing the entire form to the function...? Which itself does the desired methods...!

rowgram
December 7th, 2005, 02:19 PM
That's a good idea - I've tried it 2 things :

void StandAloneFunction(System::Windows::Forms::Form* ff)
{
ff->{???}->set_Text(S"hello");
}

void FooForm::SomeMethod()
{
StandAloneFunction(Form2);
}

but in this case, how do I access the TextBox object (which is in System::Windows::Forms, and not System::Windows::Forms::Form) ??


I've also tried :

void StandAloneFunction(mynamespace::Form2* ff)
{
ff->textBox1->set_Text(S"hello");
}

void FooForm::SomeMethod()
{
StandAloneFunction(textBox1);
}
but in this case, I get error C2039 : Form2 is not member of mynamespace, yet Class View, Object Browser, & ContextHelp all tell me that Form2 is indeed a member of mynamespace ?

I'm hoping one of these 2 approaches will allow me to pass Form2 pointer to StandAloneFunction.