Hi,
I'm assuming that you have two forms in your project MainForm and DialogForm. Main form shows DialogForm after some button is clicked:
private void btnOpenDialogForm_Click(object sender, EventArgs e)
{
using (var dialogForm = new FrmDialogForm())
{
if (dialogForm.ShowDialog() == DialogResult.OK)
{
}
}
}
On your DialogForm you have two buttons (btnSave and btnCancel). Those buttons has set properties:
btnSave.DialogResult = DialogResult.OK
btnCancel.DialogResult = DialogResult.Cancel
And your DialogForm has set properties:
DialogForm.AcceptButton = btnSave
DialogForm.CancelButton = btnCancel
You have some code for btnSave ClickEvent:
private void btnSave_Click(object sender, EventArgs e)
{
}
DialogForm is automatically closed when one of buttons is clicked (btnSave or btnCancel) after buttons click code is executed, because your form is showed as Dialog window.
If you want to prevent auto closing form you should set properties of your DialogForm:
DialogForm.AcceptButton = (none)
DialogForm.CancelButton = (none)
And buttons properties:
btnSave.DialogResult = DialogResult.None
btnCancel.DialogResult = DialogResult.None
But your form will still close (and returns DialogResult.Cancel) when user click WindowClose button (upper right corner of your form 'X').
You can control your DialogForm close using FormClosing event:
private void FrmDialogForm_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
}
Few links:
http://msdn.microsoft.com/en-us/library/39wcs2dh%28v=vs.110%29.aspx[
^]
http://msdn.microsoft.com/en-us/library/c7ykbedk%28v=vs.110%29.aspx[
^]
http://msdn.microsoft.com/library/system.windows.forms.form.closing%28v=vs.110%29.aspx[
^]
I hope it helps you :)