Search code examples
c++visual-studiowinformsvisual-c++c++-cli

Using "System::Windows::Forms::DialogResult::OK" to go back to previous form


I have two separate forms list_of_students_page and testing.

I have a button in list_of_students_page which on clicking opens testing form. I have created a button in testing form to move back to list_of_students_page.

Upon running the button in testing form, it works when it is clicked twice.

I am using C++/CLI Windows Form in Visual Studio.

list_of_students_page.h

private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {

CppCLR_WinformsProjekt1::testing^ testing_f = gcnew CppCLR_WinformsProjekt1::testing;
this->Hide();
testing_f->ShowDialog();
if (testing_f->ShowDialog() == System::Windows::Forms::DialogResult::OK)
{
    this->Show();
}
}

testing.h

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
    this->DialogResult = System::Windows::Forms::DialogResult::OK;
    this->Close();
}

Why is this happening and how should I fix this?


Solution

  • You have:

    testing_f->ShowDialog();
    if (testing_f->ShowDialog() == System::Windows::Forms::DialogResult::OK)
    

    Having the standalone ShowDialog call doesn't really make sense and does explain why you have to hit the button twice (it's actually different instances of the dialog, it just appears so quickly you can't see that).