Search code examples
c#dialogresult

Forms are not displayed - program.cs


I tried something like that :

  • Start a Form as dialog and ask user about warranty
  • If users click OK, Form is returning DialogResult.OK
  • Form1 is starting from program.cs

That is the code of my program.cs :

    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Form f = new Form2();
    if(f.DialogResult == DialogResult.OK)
    Application.Run(new Form1());

I don' t know why that doesn't work. Any form isn't shown.


Solution

  • I don' t know why that doesn't work. Any form isn't shown.

    Because you are checking the dialogresult without actually showing the form to the user and asking him to press ok or cancel.

    do it like this

    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Form f = new Form2();
    if(f.ShowDialog() == DialogResult.OK)  // note the change here.
        Application.Run(new Form1());