Form opened with ShowDialog() never closes


New to WiseJ, but I’ve been writing complex WinForms apps for over a decade.

I’m having trouble with Form.ShowDialog().  When I invoke it on a form, the form does appear, but I can’t get the form to go away.

  • Clicking the close button does nothing
  • Clicking a button that’s got DialogResult.OK and is the form’s Accept button does nothing
  • Clicking a button that’s got DialogResult.Cancel and is the form’s Cancel button does nothing

What’s the secret to making the “modal” form go away?


  • Carl Daniel
    Ah, it looks like the auto event wire-up for accept button/cancel button doesn’t work. Adding explicit button click handlers for OK/Cancel seems to do the trick.
  • You must to post comments

It’s the same in WinForms.

The AcceptButton means that when you press Enter it gets a click. The CancelButton means that when you pres Esc it gets a click. Their DialogResult becomes the DialogResult of the form. But there is no auto close, it’s up to the code to decide what to do.

  • Carl Daniel
    Not quite the same – in WinForms setting the DialogResult property on a button is sufficient to cause clicking that button to close the form invoked by ShowDialog – it’s not necessary to actually wire up a button click event handler.
  • Luca (ITG)
    Yes, you are correct. I forgot that the behavior is different in WinForms when using ShowDialog AND DialogResult is assigned. The behavior is actually from the Windows SDK for modal dialogs. I looked into old checkins and this behavior was skipped early and then we decided to keep it the same for Show and ShowDialog. Will make sure that the docs remarks are updated.
  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.