closing Modal Form inside form

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a modal form (ShowDialog()) that I may want to close when a user
clicks a button. I already have an OK and Cancel button, so I can't use
those options, but I want it to function just like an OK button press. See
Example:

private void add_Click(object sender, EventArgs e)
{
// Do some work...

if (itemcnt == 0)
{
this.DialogResult = DialogResult.Cancel;
// Close current form and return.
}
}
 
Phil,

You can just call the Close method on your form. You are specifying the
value of the DialogResult property beforehand, and this is what will be
returned from the call to ShowModal.

Hope this helps.
 
When I do this from inside my add_Click() function , I get an error:
Cannot access a disposed object.
Object name: 'ConflictList'.

The error is shown at the semicolon after I call ShowDialog(); i.e.

ConflictList dlg = new ConflictList();
dlg.ShowDialog();<- Right here

Any ideas?

Phil


Nicholas Paldino said:
Phil,

You can just call the Close method on your form. You are specifying the
value of the DialogResult property beforehand, and this is what will be
returned from the call to ShowModal.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Phil said:
I have a modal form (ShowDialog()) that I may want to close when a user
clicks a button. I already have an OK and Cancel button, so I can't use
those options, but I want it to function just like an OK button press.
See
Example:

private void add_Click(object sender, EventArgs e)
{
// Do some work...

if (itemcnt == 0)
{
this.DialogResult = DialogResult.Cancel;
// Close current form and return.
}
}
 
Hello,

Are you sure you don't have any other references of ConflictList in
dlg?

The solution that Nicholas suggested is the way to that.
Just set the DialogResult the value you want, and call this.Close().

The error you mentioned doesn't seem to have anything with the closure
of the form.

Hope this helps.
 
Back
Top