如何回到之前的表格 C++/CLI Windows 表格

How to go back to the previous form C++/CLI Windows Forms

我创建了两个表单,即主页和数据页,我需要使用按钮切换到表单。从主页我使用以下代码导航到数据页:

DataPage^ page = gcnew DataPage();
page->ShowDialog();
this->Hide();

在数据页中,我需要在单击按钮 "Home" 后返回主页。我尝试使用以下代码实现它:

public ref class DataPage: public System::Windows::Forms::Form
{
private: System::Windows::Forms::Form^ otherPage;


public: 
    DataPage(void)
    {
    HomePage: System::Windows::Forms::Form ^ home;
        otherPage = home;
        InitializeComponent();
        //
        //TODO: Add the constructor code here
        //
    }

    private: System::Void btn_home_Click(System::Object^  sender, 
    System::EventArgs^  e) {
    this->Hide();
    otherPage->Show();
    }

但是我一直收到错误 "object reference not set to an instance of an object"。请帮我解决这个问题。谢谢

p.s。我是 Visual C++ 新手

那是因为你从未初始化你的 home 变量,你只是声明它然后使用它,使 otherpage public 在你的第一个代码片段中将它分配为 page->otherpage = this; 然后使用其他页面 otherpage->show()