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();
}
单击取消按钮时出现异常:“对象未设置为实例”。
有人可以帮助我吗?
谢谢。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为
RegisterForm
创建一个构造函数,它接受System::Windows::Form ^
对象,并在实例化它时将this
传递给它在登录表单类中假设登录表单对象在 RegisterForm 类中被称为
otherform
。一旦您准备好将其恢复,只需调用otherform->Show()
当您隐藏表单时,它仍然存在,只是对用户不可见。
编辑:我让它工作得很好。以下是我对表单
Form2(注册表单)
}
Form1(登录表单)所做的修改(不是完整代码)
Create a constructor for
RegisterForm
which accepts aSystem::Windows::Form ^
object, and passthis
to it when you are instantiating it from within the login form classAssume the login form object is called
otherform
within the RegisterForm class. Once you're ready to bring it back, just callotherform->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)
}
Form1(Login form)
因此,我想展示我的完整代码以帮助其他人:-
Form2.h
Thereby , I would like to show my full code to help the others:-
Form2.h
}
}