【问题标题】:How to invoke/call performClick of menuitem of an MDI parent from another form?如何从另一个表单调用/调用 MDI 父菜单项的 performClick?
【发布时间】:2014-08-22 04:37:13
【问题描述】:

我有一个问题,很抱歉这个新手问题(只是 2 周的 Visual c++ 编程经验)。我目前正在使用 Visual C++2010 中的 MDI,这是程序的工作原理,一旦您执行应用程序,MDIParent 会以最大化状态加载,然后 MDIParent 具有 MenuStrip 并且其子菜单之一被命名为 noviceToolStripMenuItem。如果使用鼠标按钮单击 noviceToolStripMenuItem,它将打开一个名为 frmNovice 的子窗体,这很好,现在 frmNovice 包含一个简单的游戏,根据它的预期功能运行,一旦游戏结束,一个名为 frmRetry 的新窗体将被显示,但这次 frmRetry 不是子窗体,frmNovice 将关闭。 frmRetry 有两个按钮,是和否,如果用户单击否,frmRetry 将关闭,然后 MDIParent 将再次保持应用程序的焦点,但如果用户单击是按钮,应用程序应执行单击事件/功能noviceToolStripMenuItem,再次显示 frmNovice,但这是问题所在,我无法完成此操作。 frmRetry(不是子窗体)调用或调用MDIParent 的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"

提前致谢

【问题讨论】:

  • 使用 ShowDialog() 代替。
  • 我已经做了 dat,但它与 Show() 的工作原理相同,并且它们具有相同的行为

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


【解决方案1】:

您应该在创建新手表单时传入对主窗口的引用,并将该引用再次传递给重试表单。然后,让主窗口实现一个 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() 的公共方法。 Retry 表单在用户单击“是”按钮时调用它,而主窗口在用户单击其noviceTooltipMenuItem 时执行它所做的任何事情。

【讨论】:

  • 我应该将 InoviceParent.h 创建为新的头文件吗?有这个代码: public interface class InoviceParent { public: void ShowNoviceForm(); }
  • 是的,这是我的建议。
  • 我仍然无法完成这项工作,我在使用#include 时遇到了错误
  • 您应该编辑您的问题以显示新错误,或者在新问题中询问它们。
  • //这是出现错误的部分-错误如下: //Error 11 error C2664: 'MemoryGame::frmNovice::frmNovice(INoviceParent ^)' : cannot convert parameter 1 from ' MemoryGame::Form1 ^const ' to 'INoviceParent ^' c:\users\zherwyndbest\desktop\memorygamec++\memorygametpl\memorygame\memorygame\mdiMain.h 27 1 MemoryGame // 还有这个:错误 2 错误 C2039: 'ShowNovice' : is不是“InoviceParent”的成员 c:\users\zherwyndbest\desktop\memorygamec++\memorygametpl\memorygame\memorygame\frmRetry.h 142 1 MemoryGame
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-19
  • 2015-06-19
  • 2012-02-24
相关资源
最近更新 更多