I have started out with tasks that need to communicate between different forms.
I need to understand the logic how this works and have problems to find any examples of this.
For the 2 codeexamples below, I use these to close the current Form or activate another button on the same Form.
In the 2 examples, ->this is used to identify the current Form.
For example I cant write the below code on Form1 to close Form2:

Form2->Close();

What I now wonder and logic for, is if I open Form2 from Form1 with the code below.
How is it possible to activate a button on Form2 with a button on Form1 and/or how is it possible to close Form2 from Form1.
I will be happy for any logics, links how this works.

I use this code to open Form2 from Form1.

Form2 ^form2 = gcnew Form2;
form2->Owner::set(this);
form2->Show();

The 2 code example below work when using them on Form2.

//A button will activate another buttons code (button1_Click) on the same Form.
button1_Click(this, EventArgs::Empty); 


//A button can use this code to close this Form
this->Close();

Recommended Answers

All 7 Replies

Jennifer,

I believe what you want is a public function on Form2's class. For example, you can make a public function called "CloseForm" on Form2. Inside "CloseForm" you would call the close function:

this->Close();

Then, from Form1, when your button is pressed, you would issue the call:

form2->CloseForm();

Hope this helps!

Thanks for this answer.!
This I have declared in Form1: (I will close Form1 from Form2)

public: System::Void FormClose10(System::Object^ sender, System::EventArgs^ e)
{ 
   this->Close;
}

On Form2 I write this inside of a buttoncontrol. I have to #include "Form1.h" in order to find ::FormClose10.
Then when I compile this, the compiler says that:
'FormClose10' : is not a member of 'Form1'
'FormClose10': identifier not found

#include "Form1.h"

Form1::FormClose10(); //I can find the member like this. Form1-> will not find any members.

........................................................................................

I also noticed that If I only #include "Form1.h" without writing
Form1::FormClose10();
I will have this compile error:
'Form2' : undeclared identifier

Jennifer,

OK, a few things:

In Form1, if you don't want to, you don't have to pass "sender" and "e" to the public function. So the function declaration in the form can just be:

public void FormClose10();

In Form2, you will need an instance of Form1 (I'm paraphrasing from your first post, where you declared a pointer to the form), something like this:

In Form2:

Form1 *myForm1 = new Form1();
...
<do stuff with the Form1>
...
myForm1->FormClose10();

The key is that you have to have a reference variable (in this case "myForm1") in order to call the public function.

In this example:

Form1::FormClose10();

This is trying to call a static version of the function, which is not what you want. The form has to be instantiated, which is what the call:

Form1 myForm1 = new Form1();

does. Then you can monkey with it.

Let me know if I can be of more help.

Thanks Mike,

I am still a bit confused. I understand when you explain and find it logic to have a pointer to Form1 in Form2.

In Form1 I have declared this then:

public: System::Void FormClose10()
{
    this->Close();
}

Then in Form2 inside a button I write this. I have changed:
Form1 *myForm1 = new Form1();
to this. I beleive I have to write ´gcnew´
I have tried these 3 versions of the First Line but all shows "undeclared identifier"

Form1 *myForm1 = gcnew Form1; //'myForm1' : undeclared identifier
Form1 ^myForm1 = gcnew Form1; //'myForm1' : undeclared identifier
Form1 &myForm1 = gcnew Form1; //'myForm1' : undeclared identifier

myForm1->FormClose10();

I wonder if I am doing this correct.
Thanks...

Jennifer,

OK, a few things:

In Form1, if you don't want to, you don't have to pass "sender" and "e" to the public function. So the function declaration in the form can just be:

public void FormClose10();

In Form2, you will need an instance of Form1 (I'm paraphrasing from your first post, where you declared a pointer to the form), something like this:

In Form2:

Form1 *myForm1 = new Form1();
...
<do stuff with the Form1>
...
myForm1->FormClose10();

The key is that you have to have a reference variable (in this case "myForm1") in order to call the public function.

In this example:

Form1::FormClose10();

This is trying to call a static version of the function, which is not what you want. The form has to be instantiated, which is what the call:

Form1 myForm1 = new Form1();

does. Then you can monkey with it.

Let me know if I can be of more help.

Why are you doing this->Close(); ? You can just say Close(); if you're within the scope of the function, can't you? (unless you're trying to specify that you're not referring to some external Close() function, but I don't see how the compiler would think you mean that over a member of the class).

Jennifer,

OK, here's a complete rundown:

I'm assuming you are working with a "Windows Forms" app. If you call the form that you get when you start the project "Form1", this code will show you how to open a new form, and close it by pressing a button on Form1.

First, add another Windows form to the project; call it "Form2".

In that form, add a function, called FormClose10:

public:
void FormClose10()
{
[INDENT]this->Close(); // CoolGamer48 is correct, you can just say "Close()" if you want - I'm wordy...[/INDENT]
}

In the Form1 designer, add 2 buttons, one labeled "Open" and another labeled "Close".

In the Form1.h file, put the following code:

#include "Form2.h"

right after the "#pragma once" at the top of the file.

In Form1.h, inside the class definition, add the following code:

private:
[INDENT]Form2 ^myForm2; // this is different than what I first told you -- sorry

[/INDENT]

In the "Open_Click" routine, add the following code:

myForm2 = gcnew Form2();
myForm2->Show();

In the "Close_Click" routine, add the following code:

myForm2->FormClose10();

That should do it. When you run the program, Form1 will come up with 2 buttons; press "Open" and Form2 should appear... press "Close" on Form1 and Form2 should disappear.

If you have any problems, let me know. I can send a C++ project to you that has this code working if you like.

mcriscolo,

Thank you very much for the help. Now I understand the logic of how the communication goes between the Forms.
What mostly did it was this.

private:
Form2 ^myForm2;

Before I declared it inside the button instead of inside the FormClass. So with this I get a reference to Form2 and thereby I can access members to Form2.
So it works very fine now. I can close Form2 from Form1.
Thanks !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.