To add to what @user3185569 wrote:
You can set the
Form.CancelButton
andForm.AcceptButton
properties for yourForm3_addrow
form to point to yourCancel
andOK
buttons respectively. In that case you don't even needClick
event handlers, the form will simply set theDialogResult
automatically when you click on either of the buttons.You don't have to invoke
Close
on the dialog instance manually, but simplyDispose
it when you're done.
A usual way to use a modal dialog is through the using
directive, to ensure that it's disposed immediately after use:
using (var addRowForm = new Form3_addrow(tran_in))
{
var result = testDialog.ShowDialog(this);
if (result == DialogResult.OK)
{
// no need to call Close here
DoStuff();
}
}
The reason you need to do this is that a modal dialog (i.e. one shown using ShowDialog()
as opposed to just Show()
) merely hides itself when you close it (or set the DialogResult
property to a value), to allow you to access its properties even when it's not being shown anymore, as explained on MSDN.