0
votes

I have been looking for help everywhere. So far havent found the help i need.

I hope you guys can guide or help me =)

I have a Form1, on form1 i have included in Form1.h the second forms file: Form2.h So now i have created, which runs when a button is pressen:

| Form1.h | Form2^ frmProSog = gcnew Form2(); frmProSog->ShowDialog();

Now Form2 is open and i use it to search for a string in a datagrid. When i have selected the result, I will click on a button in Form2 which I want to call a function in Form1 which will add the data in another datagrid which is in Form1.

I have tried to include Form1.h in Form2 but i get an error: cannot be called with the given argument list argument types are: (System::Object ^)

I hope that someone out there is able to help me. Tried many of the following suggestions on stackoverflow which reminds of this question but no luck.

1
You created a circular dependency between these classes. Form1 needs to know what Form2 looks like. And now Form2 needs to know what Form1 looks like. This is not impossible to deal with in the C++ language but it certainly doesn't make it easy. One of the bigger reasons that Winforms project templates were removed from VS2012. You should really consider using C# instead, it uses a multi-pass compiler. You'd get ahead in C++/CLI by breaking that dependency. You do so by declaring an event in your Form2 class. Form1 can subscribe it. - Hans Passant
An event? Isn't putting the method bodies into .cpp files the standard way to handle this? - David Yaw

1 Answers

0
votes

(David Yaw's comment is correct, I write the following on the basis that the OP might need more explicit instructions. This answer is just pointing out usual C++ techniques, not something particularly specific to CLI/C++ or WinForms.)

It seems to me from your description that the body of your button-press event functions must have been in the respective header files. Furthermore, that #include "Form1.h" is in Form2.h, and vice-versa: this causing the circular dependency and associated pain.

The simple fix is to implement the event-handling methods in the Form1.cpp and Form2.cpp source files instead. Once the form designer creates the empty method...

public ref class Form 1 : System::Windows::Forms::Form
{

     private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
     {

     }
}

... then remove the implementation from the header.

public ref class Form 1 : System::Windows::Forms::Form
{

     private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) ;
}

Copy/Paste into the cpp file, and add the handler body

System::Void Form1::button1_Click(System::Object^  sender, System::EventArgs^  e) {
    Form2^ frmProSog = gcnew Form2(); 
    frmProSog->ShowDialog();
}

Remove the respective #include from the .h and put into the .cpp instead.

(If there is a member of Form1 which is a Form2^, forward declare Form2 in Form1.h)

The above will work because, on the basis of your question, there is no visual representation (i.e., in the form designer) of Form1 in Form2, or Form2 in Form1. In this case, you can have as much circular dependency as you like between forms/controls, as long as you forward declare in the *.h, and only "use" (access members, etc) circularly-dependent classes in source files. You can happily take handles (^) to Form2 in Form1.h with forward declaration, including Form2^ as member functions and method signatures.