如何调用/调用从另一个表单执行MDI父项的menuitem的执行单击?

时间:2014-07-01 14:52:04

标签: c++ visual-studio visual-c++ c++-cli

我有一个问题,抱歉这个新手问题(只需2周的视觉c ++编程经验)。我目前正在使用Visual C ++ 2010中的MDI,这里是程序的工作原理,一旦执行应用程序,MDIParent加载最大化状态,然后MDIParent有MenuStrip,其中一个subMenu被命名为noviceToolStripMenuItem。如果使用鼠标按钮点击noviceToolStripMenuItem,它将打开一个名为frmNovice的子表单,这很好用,现在frmNovice包含一个简单的游戏,根据其预期的功能,一旦游戏结束了一个名为frmRetry的新表单将显示,但这次frmRetry不是一个子窗体,frmNovice将关闭。 frmRetry有两个按钮,Yes和No,如果用户单击No,frmRetry将关闭,MDIParent将再次保持应用程序的焦点,但如果用户单击Yes按钮,应用程序应执行click事件/功能noviceToolStripMenuItem,再次显示frmNovice,但这是问题所在,我无法完成这项工作。 frmRetry(不是子表单)调用或调用MDIParent的click事件noviceToolStripMenuItem。有没有办法做到这一点,或者其他解决方案?提前谢谢。

这是代码(有些被删除以最小化空间):

**FILE: mdiMain.h**

    #pragma once
    #include "frmNovice.h"
    namespace MemoryGame {
        using namespace System;
        using namespace System::ComponentModel;
        using namespace System::Collections;
        using namespace System::Windows::Forms;
        using namespace System::Data;
        using namespace System::Drawing;

        public ref class Form1 : public System::Windows::Forms::Form {
        public:
        Form1(void)
        {
        InitializeComponent();
        //TODO: Add the constructor code here
        }

        protected:
        ~Form1()
        {
        if (components)
         {
            delete components;
         }
        }
        private: System::Windows::Forms::MenuStrip^  menuStrip1;
        protected: 
        private: System::Windows::Forms::ToolStripMenuItem^  noviceToolStripMenuItem;

       private:
       System::ComponentModel::Container ^components;

    #pragma region Windows Form Designer generated code
        void InitializeComponent(void)
        {
        this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
        this->selectLevelToolStripMenuItem = (gcnew    
            System::Windows::Forms::ToolStripMenuItem());
        this->noviceToolStripMenuItem = (gcnew 
        this->selectLevelToolStripMenuItem->Name = L"selectLevelToolStripMenuItem";
        this->selectLevelToolStripMenuItem->Size = System::Drawing::Size(70, 20);
        this->selectLevelToolStripMenuItem->Text = L"New &Game";
        // 
        // noviceToolStripMenuItem
        // 
        }
    #pragma endregion
    private: System::Void noviceToolStripMenuItem_Click(System::Object^  sen....) {  
        //OPENING NEW FORM AS MDI CHILD
        frmNovice^ newMDIChild = gcnew frmNovice();
        // Set the Parent Form of the Child window.
        newMDIChild->MdiParent = this;
        // Display the new form.
        newMDIChild->Show();
          }
    };
    }

文件:mdiMain.cpp

#include "stdafx.h"
#include "mdiMain.h"
using namespace MemoryGame;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
    // Enabling Windows XP visual effects before any controls are created
    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false); 

    // Create the main window and run it
    Application::Run(gcnew Form1());
    return 0;
}


**FILE: frmNovice.h**

#pragma once
#include "frmRetry.h"
#include "algorithm"

namespace MemoryGame {
    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;
    public ref class frmNovice : public System::Windows::Forms::Form
    {
    public:
    frmNovice(void)
    {
    InitializeComponent();
    //TODO: Add the constructor code here

    }
    protected:
    ~frmNovice()
    {
        if (components)
        {
            delete components;
        }
    }
          //codes for the UI of the game goes here.....
#pragma region Windows Form Designer generated code
     //MY USER DEFINED FUNCTION START
    void gamefunction() //this function is where calls the frmRetry to show
        {
    frmRetry^ form = gcnew frmRetry();
    form->Show(); //This time not a CHILD FORM
    this->Close();
    }

void InitializeComponent(void)
    {
        //initialize UI components GOES HERE
     }
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
    //DIFFERENT PROCEDURE FUNCTIONS GOES HERE Also invokes/calls the game function that     
        //show the frmRetry (this is working properly)
};
}

文件:frmNovice.cpp

#include "StdAfx.h"
#include "frmNovice.h"
//there are variables used for gameplay
int Choice1; 
 int Choice2;
 int Mistakes;
 int TimeRecord;
 int AllItems;

并且最后 文件:frmRetry.h

#pragma once
namespace MemoryGame {
    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;

    public ref class frmRetry : public System::Windows::Forms::Form
    {
    `enter code here`public:
    frmRetry(void)
    {
    InitializeComponent();
    }
    protected:
    ~frmRetry()
    {
    if (components)
    {
        delete components;
        }
    }
    private: System::Windows::Forms::Label^  label1;
    private: System::Windows::Forms::Button^  btnYes;
    private: System::Windows::Forms::Button^  btnNo;
    protected: 
    private: System::Windows::Forms::PictureBox^  pictureBox1;

    private:
    System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
    void InitializeComponent(void)
    {
    CODES for UI inialization goes here
    }
#pragma endregion
    private: System::Void btnNo_Click(System::Object^  .....e) {
     this->Close();
    }
private: System::Void btnYes_Click(System::Object.....^  e) {
    **THIS IS THE PART IM STuck WITH, i CANT GET THIS DONE**
        **EVErything is working fine, except for this part where i have to                      
        call again the frmNovice as an child form of the mdiParent**
         THE following are my attempts but didnt work.
           // frmRetry^ form = gcnew frmRetry();
           // frmNovice^ frmNovice=gcnew frmNovice();
       // noviceToolStripMenuItem->PerformClick();
       //frmNovice->Show();  
     }
};
}

文件:frmRetry.cpp

#include "StdAfx.h"
#include "frmRetry.h"

感谢提前

1 个答案:

答案 0 :(得分:0)

创建初学者表单时,应该传入对主窗口的引用,并将此引用再次传递给“重试”表单。然后,让主窗口实现一个NoviceParent接口,该接口包含一个用于调用Retry表单的公共方法。主窗口上的noviceToolstripMenuItem_Click方法也将委托给这个新的公共方法。

新手父接口

public interface class INoviceParent
{
 public:
      void ShowNovice();
}

主窗口

#include "INoviceParent.h"

public ref class Form1 : public System::Windows::Forms::Form, INoviceParent {
 public:
   // ... existing public methods
     void ShowNovice() {
        //OPENING NEW FORM AS MDI CHILD
        frmNovice^ newMDIChild = gcnew frmNovice(this);
        // Set the Parent Form of the Child window.
        newMDIChild->MdiParent = this;
        // Display the new form.
        newMDIChild->Show();
      }

 private:
     void noviceTooltipMenuItem_Click( /* args */) {
         ShowNovice();
     }
 // ...
};

新手表格

#include "INoviceParent.h"

public ref class frmNovice : public System::Windows::Forms::Form
{
public:
    frmNovice(INoviceParent ^ const parent)
    {
         this->parent = parent;
         // ...
    }

// ...

private:
    void gamefunction() //this function is where calls the frmRetry to show
    {
         frmRetry^ form = gcnew frmRetry(parent);
         form->Show(); //This time not a CHILD FORM
         this->Close();
    }
    INoviceParent ^parent;
}

重试表格

#include "INoviceParent.h"

public ref class frmRetry : public System::Windows::Forms::Form
{
public:
    frmRetry(INoviceParent ^ const noviceParent)
    {
        this->noviceParent = noviceParent;
        // ...
    }

    // ...
private: System::Void btnYes_Click(System::Object.....^  e) {
   noviceParent->ShowNovice();  
 }

 INoviceParent ^noviceParent;
};

这里重要的是子表单不需要与主表单或彼此强耦合。他们所知道的是,在构造函数中传递的对象有一个名为ShowNovice()的公共方法。当用户单击“是”按钮时,“重试”表单会调用它,主窗口会在用户单击其noviceTooltipMenuItem时执行任何操作。