2
votes

In my form1, I open form2 and write the result of form2 to a variable DialogResult. After closing form2, depening on the result of form2 (DialogResult), I want to reshow my form 1.

The form1 shows for a split second, and then closes.

The variable is correctly read in form1 (checked with messageboxes), but after "Show()" the form closes again. Shouldn't the form keep being shown until "Close()" is called?

Startup Code:

using BonnenPrinten;
using Ridder.Common.Script;
using System.Diagnostics;
using System.Windows.Forms;

public class RidderScript : CommandScript
{
    public void Execute()
    {
        int nestingNaam = 0;
        Process[] processes = Process.GetProcesses();
        foreach (var item in processes)
        {
            string itemnaam = item.MainWindowTitle.ToString();
            if (itemnaam.Contains("PN4000"))
                int.TryParse(itemnaam.Substring(3, 5), out nestingNaam);      
        }

        var form1 = new Form1(this, nestingNaam);
        form1.ShowDialog();
    }
}

Code in form1:

private void BtnStarten_Click(object sender, EventArgs e)
    {
        if (checkbox1.Checked)
            DeleteTijdelijkeBonnen();

        Hide();

        string sqlQuery = SetSqlQuery();
        if (checkbox2.Checked)
            sqlQuery = SetSqlQuery(txtboxNestingnaam.Text);               

        Form form2= new Form2(_script, sqlQuery, bonTekeningCombineren.Checked);
        form2.ShowDialog();

        if (form2.DialogResult == DialogResult.OK) //form2 is closed, form1  should be closed
        {
            Close();
            MessageBox.Show("Bonnenverwerking succesvol afgerond!", "Gereed", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        else //form2 is closed, form1 should be shown;
        {
            Show();
        }
    }

Code exiting form2:

DialogResult = DialogResult.OK;
Close();

When DialogResult = OK, the form should be closed.

When DialogResult = Cancel, the form should be opened.

2
Do this: Create a new WinForms application. Add a Form2. Add a button to Form1. Add a button click event handler that does the following: this.Hide(); var f2 = new Form2(); f2.ShowDialog(); this.Show();. Can you reproduce your issue? I just tried doing exactly that and I can't, so the issue must be somewhere in your logic, or in the form closing/closed event handlers of Form2.Llama
A form showing for a split second and then vanishing suggests that the variable holding the reference to the form is going out of scope at the end of a method.Rikalous
@Rikalous - Does it? An open form doesn't automatically get disposed if a reference to it goes out of scope.Enigmativity
Reproduced the problem in new project. The program remains active, but form1 flashes for a second. this.Hide(); using (var f2 = new Form2()) { f2.ShowDialog(); if (f2.Variabel == DialogResult.OK) this.Show(); }Jannick Breunis
Change DialogResult = DialogResult.OK; to DialogResult = DialogResult.Cancelpreciousbetine

2 Answers

1
votes

The problem was in the DialogResult of Form1. After opening and closing Form2, the Form1.DialogResult was also set to DialogResult.Cancel.

After searching, with pressing buttonStarten, the DialogResult was set.. Never knew this was even an option. This is removed and problem resolved.

So:

  • Form1 hide
  • Form2 show
  • Form2 close
  • Form1 show
  • Form1.Dialogresult = Cancel, so: close.

Solution: remove Button.DialogResult

Thanks for the help.

0
votes

You need to change your code like this:

        if (form2.DialogResult == DialogResult.OK)
            MessageBox.Show("Bonnenverwerking succesvol afgerond!", "Gereed", MessageBoxButtons.OK, MessageBoxIcon.Information);

        Show();

Your else was preventing Show() from being called when form2.DialogResult was DialogResult.OK.