am calling Form2 from Form1, and depending on the input in Form2, I want to enable/disable buttons in Form1 when I exit. However, because I need to call Form2 header before I define class Form1, Form1 is not defined when the compiler sees my call to it. If I therefore call Form1 header in Form2 header, then the compiler will see my call to Form2 before Form2 is defined!!.

Basically therefore, I wanna be able to call each form from each form, but have to define and make the call in one first.

This seems to me a very obvious thing to want to do, but I can't figure out myself how to do it, as you can't declare an extern class, or see any examples on the net of how to do it in Visual C++.net.

Any help appreciated, as is a real stumbling block for me.

UltimateNewbie

Dani AI

Generated

— this is a very common compiler/architecture question. Two practical, low‑coupling ways to solve it depending on whether Form2 should be a modal dialog (you only care about its final result) or a non‑modal live window.

For a modal dialog (simplest for "enable/disable when I exit"): have Form2 expose the choice as a public property and set DialogResult before closing. From Form1, show Form2 with ShowDialog and read that property after the dialog returns:

Form2^ dlg = gcnew Form2();
if (dlg->ShowDialog() == System::Windows::Forms::DialogResult::OK)
{
    button1->Enabled = dlg->EnableButton; // public property on Form2
}
delete dlg; // calls Dispose in C++/CLI; optional but recommended for Windows Forms

For a non‑modal scenario where Form2 can change state while open, use events/delegates so Form2 notifies Form1 when something changes. Subscribe from Form1 and update controls in the handler — this keeps coupling low and avoids circular header problems:

Form2^ f2 = gcnew Form2();
f2->OptionChanged += gcnew System::EventHandler(this, &Form1::OnOptionChanged);
f2->Show();

// handler in Form1:
void OnOptionChanged(Object^ sender, System::EventArgs^ e)
{
    button1->Enabled = safe_cast<Form2^>(sender)->EnableButton;
}

A few implementation tips tied to earlier replies: was right that forward declarations help — for native C++ use class Form2;, and for managed C++/CLI use ref class Form2; if you must forward-declare. Prefer placing actual #include lines in .cpp files (not headers) or move shared interfaces/properties into a small common header to avoid circular includes. Use properties/events instead of having forms directly manipulate each other’s internals — it’s cleaner and easier to maintain.

Recommended Answers

All 4 Replies

Well, I can think of a few solutions. One is to have a parent class of "AForm" that both Form1 and Form2 inherit from. Then both can refer to the .h file that AForm is defined in, no sweat.

Another is to use a forward declaration of form 2 in form 1's header file.

class Form2; // forward declaration
. . .
class Form1
{
public:
    TellForm1Something( Form1* pForm1 );
};

In this case, the forward declaration allows you to refer to a pointer to form1. You can't refer to form1 in a way that would require the compiler to know its size or contents, of course, but pointers to form1 are ok.

Hope this helps!

Oops! Too early in the morning to post; need more coffee. Here's what I MEANT to say:

class Form2; // forward declaration
. . .
class Form1
{
public:
    TellForm2Something( Form2* pForm2 );
};

Sheesh. Naru was right about me! Lazy AND stupid!

Sheesh. Naru was right about me! Lazy AND stupid!

There is no need to take Naru's comments too serious.

>There is no need to take Naru's comments too serious.
Anyone who takes me serious here is lazy, stupid, and foolish. :D;)

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.