Question

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.

Était-ce utile?

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

Autres conseils

You never call ShowDialog on f of type Form2.

How it works, without not showing anything as modal.

A DialogResult that represents the result of the form when used as a dialog box.

I know this question is very old, but for future search.

The name you have given your form name will appear in program.cs. Make sure of the correct name in the form.

namespace Yournamespace
{
public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
    }

Note that Form1 might be something different and that name will appear in program.cs

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top