Search code examples
c++-clicircular-dependency

Stuck on circular dependency on windows forms C++


I believe that the problem is that my code is stuck on circular dependency. I'm trying to send value from one form to other. In form1 I declare Form2 and open it and therefore I can't declare Form1 from Form2 and I don't know what to do now.

Code:

private: System::Void paieškaToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e)
{
    PaieskaGUI^ paieska = gcnew PaieskaGUI(); //declaring the other form
    paieska->Show(); //opening it when the button is pressed
}

So above is the form1 where when I press the button and it opens form2. And in the code below which shows form2 when I press the button it declares form1 and sends the value to it

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) 
{
    String^ mas = textBox1->Text;
    Pagrindinis^ pgrr = gcnew Pagrindinis();
    pgrr->SomeText = mas;
    this->Hide();
}

I'm new at programming.


Solution

  • If I understand correctly you are looking to pass a value back to the original form and not create a new instance of one. If that is the case, maybe consider setting the Parent on form2 before you show it so that you can get the first form from the Parent property and then set the relevant value on it after casting it to the appropriate type.

    In the code you are also hiding the second form after the button is clicked, do you intend for the form to just be a dialog to get some value and return it to the parent? If so, maybe you want to consider using ShowDialog instead which would prevent interaction with the parent until returning and then you could use the instance of the second form from the calling method to access properties on that form. The example on MSDN for the Form.ShowDialog method shows how you can get a value from a second form to the first form.