C ++ / CLI - 如何打开一个新的表单和回来 [英] C++/CLI - how to open a new form and back
问题描述
我创建一个应用程序,前端必须是使用C ++ / CLI的Windows窗体。该表单用于登录目的。
在我的表单中,我有一个注册按钮。点击这个按钮,一个新的窗体应该打开(关闭登录窗体)。我可以通过以下代码实现这一点:
Form ^ rgForm = gcnew RegisterForm;
rgForm-> Show();
this-> Hide(); // using this-> Close()关闭应用程序
现在我想要取消按钮,其单击应再次打开登录表单并关闭注册表单。我如何实现呢?
(我对使用this-> Hide()感到困惑,是否意味着表单存在,我们只是没有显示它,因此即使在注册表单可见性之后,登录表单仍然存在?)
更新:现在当前窗体句柄被传递到注册表单构造函数变量在RegisterForm类中的名称为loginForm)。
以下是取消按钮点击的代码:
// RegisterForm类构造函数
RegisterForm(System :: Windows :: Forms :: Form ^ f)
{
loginForm = f;
}
//取消按钮点击
private:System :: Void BtnCancel_Click(System :: Object ^ sender,System :: EventArgs ^ e)
{
loginForm-> Show();
this-> Hide();
}
取消按钮单击我得到异常:对象未设置为实例
有人可以帮助我。
谢谢。
System :: Windows :: Form ^
对象,并且当您在登录表单类中实例化时,将
传递给它 Form ^ rgForm = gcnew RegisterForm(this);
rgForm-> Show();
this-> Hide();
假设登录表单对象被调用 otherform
在RegisterForm类中。一旦准备好恢复,只需调用 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登录表单)
#includeForm2.h
private: :Void Register_Click(System :: Object ^ sender,System :: EventArgs ^ e){
Form2 ^ frm2 = gcnew Form2(this);
frm2-> 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.
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();
}
这篇关于C ++ / CLI - 如何打开一个新的表单和回来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!