Question

I need to put a ToolStripMenuItem on Enabled from a Child Form in C++ .NET. I try to search this on internet, but nothing for C++, all the answers ware for C#.

I try this but don't work (menu_open is the ToolStripMenuItem ):

this->MdiParent->Controls["menu_open"]->Enabled=true;

I try:

(Form1)this->MdiParent->Controls["menu_open"]->Enabled=true;

and

((Form1)this->MdiParent)->Controls["menu_open"]->Enabled=true;

but don't find the Form1 that is the parent. Please help.

I try this: add the line

ref class Form1;

inside the namespace of the child form and the line

 Form1^ parent;

inside of the public ref class Child .. . Now I have an object of parent form and I try:

parent->Controls["menu_open"]->Enabled = true;

But I have this errors:

   use of undefined type 'DataLogger::Form1'
   left of '->Controls' must point to class/struct/union/generic type
   left of '->Enabled' must point to class/struct/union/generic type
   use of undefined type 'DataLogger::Form1'       
   left of '->Controls' must point to class/struct/union/generic type
   left of '->Enabled' must point to class/struct/union/generic type
Était-ce utile?

La solution

If enyone will have the same problem, I found the answer. You will need to use the MdiChildActivate event of the parent form. This event will appear when the child is created and when the cild is closed, to resolve this probleme, use this code:

Create a private varible outside the function set on 0:

private: static int closeChild = 0;

In private: System::Void Form1_MdiChildActivate(System::Object^ sender, System::EventArgs^ e) use this code:

closeChild++;
String ^ childName = this->MdiChildren[0]->Text;

if(closeChild == 2)
   menu_open->Enabled=true;

closeChild will be 2 on closing. And don't forget to reset closeChild to 0 when a new child is created becouse closeChild will increment to 3, etc..

Good luck !

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top