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

发布于 2024-11-03 02:30:43 字数 874 浏览 1 评论 0原文

我正在创建一个应用程序,其中前端必须是使用 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();
}

单击取消按钮时出现异常:“对象未设置为实例”。

有人可以帮助我吗?

谢谢。

I am creating an application where the front end has to be a Windows Form using C++/CLI. The form is used for login purpose.

In my form, I have a register button. On click of this button, a new form should open ( closing the login form ). I was able to achieve this by the following code:

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

Now I want to have a cancel button on the register form, whose click should open the login form again and close the register form. How do I achieve that?

( I am confused with the use of this->Hide(), does it mean that the form exists, we just did not show it, and so even after the register form visibility, the login form still exists? )

Update : Now current form handle is passed into register form constructor ( storing it as a private variable with the name loginForm in RegisterForm class ).

Following is the code for cancel button click:

// 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();
}

On cancel button click I am getting the exception : "object not set to instance".

Can someone please help me.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猫瑾少女 2024-11-10 02:30:43

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

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

假设登录表单对象在 RegisterForm 类中被称为 otherform。一旦您准备好将其恢复,只需调用 otherform->Show()

当您隐藏表单时,它仍然存在,只是对用户不可见。

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

Form2(注册表单)

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();
}

Create a constructor for RegisterForm which accepts a System::Windows::Form ^ object, and pass this to it when you are instantiating it from within the login form class

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

Assume the login form object is called otherform within the RegisterForm class. Once you're ready to bring it back, just call otherform->Show()

When you hide the form, it still exists, it is just not visible to the user.

EDIT: I got this to work just fine. Here are the modifications (not the full code) that I made to the form

Form2(Registration form)

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(Login form)

#include "Form2.h"

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

             Form2 ^ frm2 = gcnew Form2(this);
             frm2->Show();
             this->Hide();
}
肤浅与狂妄 2024-11-10 02:30:43

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

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();

             }
    };






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

}

Thereby , I would like to show my full code to help the others:-

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();

             }
    };






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

}
姜生凉生 2024-11-10 02:30:43
///////////////////////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();


         }
};

}

///////////////////////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();


         }
};

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文