![]() |
| | #1 |
| Registered User Join Date: Feb 2010
Posts: 7
| Have Child, Got Parent, need Sibling 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
}
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 | |
| | #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 | |
| | #3 |
| Staff software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 6,014
| 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 | |
| | #4 | ||
| Registered User Join Date: Feb 2010
Posts: 7
| Quote:
TextBox tb = safe_cast<TextBox^>(myForm->Controls["textBox1"]); 'System::Windows::Forms::TextBox' : class does not have a copy-constructor Quote:
| ||
| motocross1 is offline | |
| | #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 | |
| | #6 | ||
| Registered User Join Date: Jan 2010
Posts: 238
| Quote:
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:
| ||
| _Mike is offline | |
| | #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
Code: SplitContainer^ sc = safe_cast<SplitContainer^>(myForm->Controls["splitContainer1"]); sc->SplitterDistance=200; // IT WORKS - YEAH! 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"]); |
| motocross1 is offline | |
| | #8 |
| Staff software engineer 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 | |
| | #9 | ||
| Registered User Join Date: Jan 2010
Posts: 238
| Quote:
![]() Quote:
![]() 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!";
}
Code: Form1
splitContainer1
panel1
button1
panel2
textbox1
| ||
| _Mike is offline | |
| | #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
}
|
| motocross1 is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |