Search code examples
winformsvisual-c++c++-cli

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


I have created two forms namely homepage and datapage and I need to switch to the forms using buttons. From the homepage i used the following code to navigate to the datapage:

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

In the datapage I need to get back to the homepage once i click the button "Home". I tried to achieve it using the following codes:

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

However I keep getting the error "object reference not set to an instance of an object". Please help me on this. thanks

p.s. I'm new to Visual C++


Solution

  • Well that's because you never initialized your home variable, you merely declared it and then used it, make otherpage public assign it in your first code snippet as page->otherpage = this; then use otherpage to otherpage->show()