Have Child, Got Parent, need Sibling

This is a discussion on Have Child, Got Parent, need Sibling within the C++ Programming forums, part of the General Programming Boards category; I use FindForm to get the Parent Object but the Object Members/Properties do not include access to the "public textBox1": ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    93

    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.

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    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"];

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    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...
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    93
    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

    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'.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    93
    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.

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    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);

    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?

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    93
    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?

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    What I meant was this...

    Code:
    Form1^ myForm = safe_cast<Form1^>( ctrl->FindForm() );
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    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

    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.

  10. #10
    Registered User
    Join Date
    Feb 2010
    Posts
    93
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why isn't the execlp() function doing anything?
    By jsrig88 in forum C Programming
    Replies: 5
    Last Post: 10-12-2009, 10:09 AM
  2. inter process communcation, parent - child
    By tallan in forum C Programming
    Replies: 5
    Last Post: 02-28-2009, 03:04 AM
  3. Replies: 3
    Last Post: 10-15-2008, 09:24 AM
  4. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

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