我正在创建一个应用程序,其中前端必须是使用 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();
}
单击取消按钮时出现异常:“对象未设置为实例”。
有人可以帮帮我吗。
Thanks.
创建一个构造函数RegisterForm
它接受一个System::Windows::Form ^
对象,并传递this
当您从登录表单类中实例化它时
Form^ rgForm = gcnew RegisterForm(this);
rgForm->Show();
this->Hide();
假设调用登录表单对象otherform
在 RegisterForm 类中。一旦您准备好取回,只需致电otherform->Show()
当您隐藏表单时,它仍然存在,只是用户看不到它。
EDIT:我让这个工作得很好。这是我对表单所做的修改(不是完整的代码)
表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();
}
表格1(登录表格)
#include "Form2.h"
private: System::Void Register_Click(System::Object^ sender, System::EventArgs^ e) {
Form2 ^ frm2 = gcnew Form2(this);
frm2->Show();
this->Hide();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)