C ++ / CLI - 如何打开新表单并返回

时间:2011-04-24 04:18:06

标签: winforms c++-cli

我正在创建一个应用程序,其前端必须是使用C ++ / CLI的Windows窗体。该表单用于登录。

在我的表格中,我有一个注册按钮。单击此按钮,将打开一个新表单(关闭登录表单)。我能够通过以下代码实现这一目标:

Form^ rgForm = gcnew RegisterForm;
rgForm->Show();
this->Hide(); // using this->Close() was closing the application

现在我想在注册表单上有一个取消按钮,其单击应该再次打开登录表单并关闭注册表单。我如何实现这一目标?

(我对使用this-> Hide()感到困惑,是否意味着表单存在,我们只是没有显示它,所以即使在注册表单可见性之后,登录表单仍然存在?)

更新:现在将当前表单句柄传递给寄存器表单构造函数(将其存储为RegisterForm类中名为loginForm的私有变量)。

以下是取消按钮点击的代码:

// RegisterForm class constructor

RegisterForm(System::Windows::Forms::Form^ f)
{
    loginForm = f;
}

// Cancel button click

private: System::Void BtnCancel_Click(System::Object^  sender, System::EventArgs^  e) 
{
     loginForm->Show();
     this->Hide();
}

在取消按钮上单击我收到异常:“对象未设置为实例”。

有人可以帮帮我吗。

感谢。

3 个答案:

答案 0 :(得分:3)

RegisterForm创建一个构造函数,该构造函数接受System::Windows::Form ^对象,并在从登录表单类中实例化时将this传递给它

Form^ rgForm = gcnew RegisterForm(this);
rgForm->Show();
this->Hide();

假设在RegisterForm类中调用登录表单对象otherform。准备好恢复后,只需致电otherform->Show()

当您隐藏表单时,它仍然存在,只是用户看不到。

编辑:我让这个工作得很好。以下是我对表单

所做的修改(不是完整代码)

表格2(报名表格)

Form2(System::Windows::Forms::Form ^ frm1)
    {
        otherform = frm1;
        InitializeComponent();

    }

private: System::Windows::Forms::Form ^ otherform;

private: System::Void Cancel_Click(System::Object^  sender, System::EventArgs^  e) {
             this->Hide();
             otherform->Show();

}

Form1(登录表单)

#include "Form2.h"

private: System::Void Register_Click(System::Object^  sender, System::EventArgs^  e) {

             Form2 ^ frm2 = gcnew Form2(this);
             frm2->Show();
             this->Hide();
}

答案 1 :(得分:1)

因此,我想展示我的完整代码以帮助其他人: -

<强> Form2.h

#pragma once

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

namespace Form1 {



    /// <summary>
    /// Summary for Form2
    /// </summary>
    public ref class Form2 : public System::Windows::Forms::Form
    {



    private: System::Windows::Forms::Form ^ otherform;
    public:
        Form2(System::Windows::Forms::Form ^ o )
        {
            otherform = o;
            InitializeComponent();

            //
            //TODO: Add the constructor code here
            //
        }

    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~Form2()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::Button^  button1;
    protected: 

    private:
        /// <summary>
        /// Required designer variable.
        /// </summary>
        System::ComponentModel::Container ^components;

        #pragma region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        void InitializeComponent(void)
        {
            this->button1 = (gcnew System::Windows::Forms::Button());
            this->SuspendLayout();
            // 
            // button1
            // 
            this->button1->Location = System::Drawing::Point(147, 132);
            this->button1->Name = L"button1";
            this->button1->Size = System::Drawing::Size(102, 38);
            this->button1->TabIndex = 0;
            this->button1->Text = L"Menu";
            this->button1->UseVisualStyleBackColor = true;
            this->button1->Click += gcnew System::EventHandler(this, &Form2::button1_Click);
            // 
            // Form2
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(412, 295);
            this->Controls->Add(this->button1);
            this->Name = L"Form2";
            this->Text = L"Form2";
            this->Load += gcnew System::EventHandler(this, &Form2::Form2_Load);
            this->ResumeLayout(false);

        }
    #pragma endregion
    private: System::Void Form2_Load(System::Object^  sender, System::EventArgs^  e) {
             }
    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
                 this->Hide();
             otherform->Show();

             }
    };






    ////////////////////////

}

答案 2 :(得分:0)

///////////////////////Form1.h
#pragma once
#include "stdafx.h"
#include "Form2.h"
namespace Form1 {

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

/// <summary>
/// Summary for Form1
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{

public:
    Form1(void)
    {
        InitializeComponent();
        //
        //TODO: Add the constructor code here
        //
    }

protected:
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    ~Form1()
    {
        if (components)
        {
            delete components;
        }
    }
private: System::Windows::Forms::Button^  button1;
protected: 

private:
    /// <summary>
    /// Required designer variable.
    /// </summary>
    System::ComponentModel::Container ^components;

   #pragma region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    void InitializeComponent(void)
    {
        this->button1 = (gcnew System::Windows::Forms::Button());
        this->SuspendLayout();
        // 
        // button1
        // 
        this->button1->Location = System::Drawing::Point(140, 206);
        this->button1->Name = L"button1";
        this->button1->Size = System::Drawing::Size(75, 23);
        this->button1->TabIndex = 0;
        this->button1->Text = L"Login";
        this->button1->UseVisualStyleBackColor = true;
        this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
        // 
        // Form1
        // 
        this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
        this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
        this->ClientSize = System::Drawing::Size(377, 291);
        this->Controls->Add(this->button1);
        this->Name = L"Form1";
        this->Text = L"Form1";
        this->ResumeLayout(false);

    }
#pragma endregion
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {

         Form2 ^ frm2 = gcnew Form2(this);
         frm2->Show();
         this->Hide();


         }
};

}