C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-10-2010, 04:51 PM   #1
Registered User
 
Join Date: Feb 2010
Posts: 7
Have Child, Got Parent, need Sibling

I use FindForm to get the Parent Object but the Object Members/Properties do not include access to the "public textBox1":

Code:
void button_click(System::Object^ sender, System::EventArgs^ e){
	Control^ ctrl = safe_cast<Control^>(sender);
	Form^ myForm = ctrl->FindForm();
	MessageBox::Show(myForm->Name,"The Name"); // works
	myForm->textBox1->Text = gcnew String("Hello"); // does not work
}
error C2039: 'textBox1' : is not a member of 'System::Windows::Forms::Form'

Yes, you can guess... I am trying to access it outside the class, which is why I do not simply use this->textBox1 - the call is made with:

this->Go->Click += gcnew System::EventHandler(button_click);

Any insights? Thanks.
motocross1 is offline   Reply With Quote
Old 02-10-2010, 05:04 PM   #2
Registered User
 
Join Date: Jan 2010
Posts: 238
I haven't used managed c++ so I don't know how much it differs from C#, but in C# you could use
Code:
TextBox tb = (TextBox)myForm.Controls["textBox1"];
_Mike is offline   Reply With Quote
Old 02-10-2010, 05:19 PM   #3
Staff software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 6,014
Quote:
Originally Posted by motocross1 View Post
Code:
Form^ myForm = ctrl->FindForm();
Instead of Form, you should be using whatever class your main form actually is. Form is just a base class, it has no such members...
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 02-10-2010, 07:48 PM   #4
Registered User
 
Join Date: Feb 2010
Posts: 7
Quote:
TextBox tb = (TextBox)myForm.Controls["textBox1"];
I not very good at converting C# to C++ I get an error with:
TextBox tb = safe_cast<TextBox^>(myForm->Controls["textBox1"]);
'System::Windows::Forms::TextBox' : class does not have a copy-constructor

Quote:
Instead of Form, you should be using whatever class your main form actually is.
I have tried directly accessing FormNamespace::Form1::textBox1 to no avail... can you give me an example of what you mean otherwise. And by the way, myForm->Name does give 'Form1'.
motocross1 is offline   Reply With Quote
Old 02-10-2010, 07:55 PM   #5
Registered User
 
Join Date: Feb 2010
Posts: 7
Actually I added a carat and it complied without errors:
TextBox^ tb = safe_cast<TextBox^>(myForm->Controls["textBox1"]);

But then crashed when I clicked the button with:
Object reference not set to an instance of an object.
motocross1 is offline   Reply With Quote
Old 02-10-2010, 08:22 PM   #6
Registered User
 
Join Date: Jan 2010
Posts: 238
Quote:
I have tried directly accessing FormNamespace::Form1::textBox1 to no avail... can you give me an example of what you mean otherwise. And by the way, myForm->Name does give 'Form1'.
What brewbuck is saying is that instead of
Form^ myForm
you should use the class that is derived from Form.
By the sound of things I'm guessing you should use
Form1^ myForm = ctrl->FindForm();
or if FindForm() is a member function of Form1 I think you can just cast 'sender' to Form1 directly
Form1^ myForm = safe_cast<Form1^>(sender);

Quote:
Actually I added a carat and it complied without errors:
TextBox^ tb = safe_cast<TextBox^>(myForm->Controls["textBox1"]);

But then crashed when I clicked the button with:
Object reference not set to an instance of an object.
Are you checking that tb isn't NULL before trying to dereference it?
_Mike is offline   Reply With Quote
Old 02-11-2010, 01:19 PM   #7
Registered User
 
Join Date: Feb 2010
Posts: 7
Tried this: ---------------------------------------------Pure-FYI-OK-to-Skip-It---------
Form1^ myForm = safe_cast<Form1^>(sender);

It complies, then crashes when button is clicked:
Unable to cast object of type 'System.Windows.Forms.Button' to type 'FormNamespace.Form1'

In case you didn't know... FindForm() is a .NET function: Control.FindForm Method (System.Windows.Forms)
I'm OK with using it to hook the parent form.

As for:-------------------------------------------------Please-Dont-Skip-This---------
Code:
TextBox^ tb = safe_cast<TextBox^>(myForm->Controls["textBox1"]);
if(!tb){ MessageBox::Show("Not tb","Alert"); return; } // I added this
Yep, when I click the button it says "Not tb"... but, since I have textBox1 inside splitContainer1, I tried:
Code:
SplitContainer^ sc = safe_cast<SplitContainer^>(myForm->Controls["splitContainer1"]);
sc->SplitterDistance=200; // IT WORKS - YEAH!
So, how can I hook textBox1 - I tried and failed with:
Code:
TextBox^ tb = safe_cast<TextBox^>(sc->Controls["textBox1"]);
TextBox^ tb = safe_cast<TextBox^>(myForm->Controls["splitContainer1->textBox1"]);
TextBox^ tb = safe_cast<TextBox^>(myForm->Controls["splitContainer1::textBox1"]);
Ideas?
motocross1 is offline   Reply With Quote
Old 02-11-2010, 02:08 PM   #8
Staff software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 6,014
What I meant was this...

Code:
Form1^ myForm = safe_cast<Form1^>( ctrl->FindForm() );
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 02-11-2010, 02:15 PM   #9
Registered User
 
Join Date: Jan 2010
Posts: 238
Quote:
Originally Posted by motocross1 View Post
In case you didn't know... FindForm() is a .NET function: Control.FindForm Method (System.Windows.Forms)
I'm OK with using it to hook the parent form.
Ah I see, thanks. I had never heard of it before so I thought it was a function you had written yourself

Quote:
Ideas?
You should have mentioned that the textbox was in a splitcontainer
Code:
System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)
{
	Control^ ctrl = safe_cast<Control^>(sender);
	Form1^ myForm = safe_cast<Form1^>(ctrl->FindForm());
	TextBox^ tb = safe_cast<TextBox^>(myForm->splitContainer1->Panel2->Controls["textBox1"]);
	tb->Text = "It works!";
}
I'm using a form layout of:
Code:
Form1
  splitContainer1
    panel1
      button1
    panel2
      textbox1
Adjust variable names to fit your project.
_Mike is offline   Reply With Quote
Old 02-11-2010, 03:06 PM   #10
Registered User
 
Join Date: Feb 2010
Posts: 7
Awesome!!

Thanks Everyone

And, in case anyone is curious... Yes it is possible to take your Form functions out of the form.h and put them into a source.cpp --if you are using VC++ 2008 Express the final trick is to attach the events in the Form1_Load so that the designer doesn't grip about a parse error:
Code:
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e){
		this->button->Click += gcnew System::EventHandler(button_click); // without "this," hence the VC++ grip
}
Then just move the event functions to where ever.
motocross1 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Why isn't the execlp() function doing anything? jsrig88 C Programming 5 10-12-2009 10:09 AM
inter process communcation, parent - child tallan C Programming 5 02-28-2009 04:04 AM
How can you make a parent process wait for a child? I'm gettin a seg fault. mr_coffee C Programming 3 10-15-2008 09:24 AM
process programming St0rM-MaN Linux Programming 2 09-15-2007 07:53 AM
Request for comments Prelude A Brief History of Cprogramming.com 15 01-02-2004 10:33 AM


All times are GMT -6. The time now is 12:11 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22