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.
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. – LlamaDialogResult = DialogResult.OK;
toDialogResult = DialogResult.Cancel
– preciousbetine