Thread: Pass value from Form3 to Form2

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    59

    Pass value from Form3 to Form2

    There is one function that I really need to know and understand how to do.
    The example is that I have is a form application with 3 Forms.
    Form1 is the starting form.

    What I need to do now is to pass a value from Form3 to Form2 and
    let Form2 Show a message with this "String".

    RealExample would be:

    Form3:
    Code:
    String^ PassThisString = "PassThisToForm2";
    
    private: System::Void button4_Click(System::Object^  sender, System::EventArgs^  e) 
    {
    		 
    	//Pass, PassThisString to Form2 ?
                      this->Close(); //After passing, close Form3
    }
    Now, the PassThisString will be send to Form2 and showed in a ex: MessageBox like:
    Code:
    MessageBox::Show(PassThisString)
    I have been wondering for a long time how you can do this the easiest way but never got a good answer on this.
    I have always been working within the form where I know how to call GlobalVariables and button, events.

    Thank you in advance

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I have been wondering for a long time how you can do this the easiest way but never got a good answer on this.
    The best solution I've found in the general case is to provide a public property within Form3 that Form2 reads.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    59
    Thank you Prelude, this is really valueble information for me to find the right path how to do this. I will have a lot of solutions and code and Forms to do when I know how to do this technique.
    But this is what prevents me to start out that project.


    What I wonder here is that I open Form3 from Form2 so I have #include "Form3.h" in Form2 wich make it possible to open Form3.
    Now, do I need to #include "Form2.h" in Form3 also in order to do this communication with the public property or is it not neccesary ?
    Because if I do that I will have compileerrors that says that:

    'Form3' : undeclared identifier.

    Second, If I will provide a public property in Form3. How will I do that, this is the logic where I get stuck.
    Is this some sort of code that I will write in Form3 somewhere that Form2 can read from.
    I dont think I know how to do this.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Form3 doesn't need to know about Form2 since Form2 is the parent. Form3 isn't going to be working with an object of Form2 or accessing any static members, right?

    >I dont think I know how to do this.
    I didn't expect you to, but you can google "C++/CLI properties" and figure it out.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    59
    Thank you Prelude, I will googling around on C++/CLI properties to see what I can come up with.
    Thanks alot...

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    59
    I tried to understand some examples of this but the examples that I read dont really explain what they use it for.
    The nearest what I can think of is some kind of example like this but are not sure if I am close to any solution.
    However I have a feeling something is very wrong.

    Here I try to assign the String^ name = "PassThisString" to get() in a messageBox in Form2.

    Written in Form3:
    Code:
    private: String^ _name;
    
    public:
    	property String^ Name
    	{
    		void set (String^ name)
    		{
    			_name = name;
    		}
    	}
    
    
    
    private: System::Void button5_Click(System::Object^  sender, System::EventArgs^  e) 
    {
    	 name = "PassThisString";
    }

    Here I try to get the value from the set String^ _name.

    Written in Form2:
    Code:
    public:
    	property String^ Name
    	{
    		String^ get()
    		{
    			return _name;
    		}
    	}
    
    
    private: System::Void button5_Click(System::Object^  sender, System::EventArgs^  e) 
    {
    	 MessageBox::Show(_name);
    }
    Last edited by franse; 11-06-2008 at 03:28 PM.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    59
    I have found something basic, (First step) where it is possible to pass a value from the parentForm to the childform where the ParentForm is Form2 and the ChildForm is Form3.

    Then you will put the value that you want to pass to the childform like this but when doing that the compiler will say that:
    'Form1::Form3' : class does not have a copy-constructor
    The example I red was for C# so I have translated that to say.
    Code:
    Form3 ^form3 = gcnew Form3(parentValue); //Pass parentValue

    Code:
    private: System::Void toolStripMenuItem2_Click(System::Object^  sender, System::EventArgs^  e) 
    {
    	  String^ parentValue  = "passThis";
    		 
    	  Form3 ^form3 = gcnew Form3(parentValue);
    	  form3->ShowDialog();
    }

  8. #8
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Did you implement that constructor to take a string?

    Code:
    public:
    Form3(String ^someValue)
    {
    }
    Woop?

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    59
    In Form2:
    Code:
    private: System::Void toolStripMenuItem2_Click(System::Object^  sender, System::EventArgs^  e) 
    {
    	  String^ parentValue  = "passThis";
    		 
    	  Form3 ^form3 = gcnew Form3(parentValue);
    	  form3->ShowDialog();
    }
    In Form3:
    Code:
    public:
    Form3(String^ someValue)
    {
    
    }
    No I did not do that. That is my problem with this, that I haven´t passed strings and values in constructors so this is quite new for me how to do that.
    I have implemented the code as above and it do compiles now.
    However when I open Form3 the whole Form is completely emty from it´s controls. I have some buttons there and a listView but the Form is completely emty.
    But if I write this line like this instead, the Form will Show with all it´s controls.
    Why does this happen.

    Code:
    Form3 ^form3 = gcnew Form3;
    Last edited by franse; 11-06-2008 at 06:52 PM.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    59
    I tried this and this seems to work, to pass the string from Parent Form to Child Form and set the passed string to a buttoncontrol where the InitializeComponent() is.
    Code:
    public: Form3(String^ initialValue)
    {
    	InitializeComponent();
    	//
    	//TODO: Add the constructor code here
    	//
            button5->Text = initialValue;
    			
    }

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    59
    In Form4:
    Code:
    public: System::Void ActivateThis()
    {
    	MessageBox::Show("Two Passed Strings ?");	 
    }
    In Form22:
    Code:
    private: System::Void button5_Click(System::Object^  sender, System::EventArgs^  e) 
    {
                     String^ FirstString = "1";
                     String^ SecondString = "2";
    
    	 Form4::ActivateThis();
    }
    Now comes my real task. Form4 and Form22 is childForms of Form1.
    What I need to learn is how I will pass 2 strings from From22 to Form4 and
    show them in the MessageBox. Both Forms is opened during the passing.

    I have tried to declared a public: System::Void that hopefully can be reached from Form22.
    The member ActivateThis() is found here: Form4::ActivateThis();
    But the compiler says:
    'Form4' : is not a class or namespace name
    'ActivateThis': identifier not found


    So I wonder if I am on the right path how to do this.
    Last edited by franse; 11-07-2008 at 10:04 AM.

  12. #12
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    You can't call methods like that unless it is declared static. You will need to instantiate an instance of form4 in form22 in order to call its public method. And you will probably want to declare ActivateThis to take two strings passed from form 22.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Perhaps I've gotten lost here, but wouldn't you need to call ActivateThis() using an object of type Form4? And I would guess that Form22 knows nothing of Form4 and vice versa.

  14. #14
    Registered User
    Join Date
    Oct 2008
    Posts
    59
    Yes that is right Form22 knows nothing of Form4 and vice versa.
    Is it really nessecary to use an object of type Form4 in this case ?

    From what I understand I would want to do something like this in that case wich could work:
    In Form22:
    Code:
    #include "Form4.h"
    
    //referenceobject to Form4:
    private: Form4 ^mForm4;
    
    private: System::Void button5_Click(System::Object^  sender, System::EventArgs^  e) 
    {
        mForm4->ActivateThis();
    }
    The problem will come when I later want to pass a string From the opened forms:
    Form4 to Form22. (The other way)

    That will meen that I have to #include "Form4.h" in Form22 and then I will have a circular #include that will not work ? The compiler complains if I do this.

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    How are you going to pass information to a Form4 if you don't have a Form4 object to pass it to?

    And certainly, when you are defining this function at least, Form4 has to be a known type -- how else would you be able to look up what function to call?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pass a List<double>^ to an opening Form
    By franse in forum C++ Programming
    Replies: 4
    Last Post: 01-07-2009, 11:32 AM
  2. Speed test result
    By audinue in forum C Programming
    Replies: 4
    Last Post: 07-07-2008, 05:18 AM
  3. Using MC++ How do you pass a String between forms?
    By Dream Theater in forum Windows Programming
    Replies: 1
    Last Post: 04-30-2005, 06:23 AM
  4. pass be reference versus pass by value
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-01-2002, 01:03 PM
  5. Replies: 3
    Last Post: 04-02-2002, 01:39 PM