Reputation: 343
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.
Upvotes: 2
Views: 284
Reputation: 343
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:
Solution: remove Button.DialogResult
Thanks for the help.
Upvotes: 1
Reputation: 117064
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
.
Upvotes: 0