C++/CLI - how to open a new form and back

后端 未结 3 1621
终归单人心
终归单人心 2021-01-15 16:44

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

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-15 17:04

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

提交回复
热议问题