-1

When I open a Form via Form.Show() the Show Event of the opened form is not raised.

But when I use Form.ShowDialog(), the event is raised and my breakpoint is hit in my Form_Shown method.

I saw this answer, that an exception hides the Event, but I get no exception.

So, why is the Event raised when I call ShowDialog(), but not if I call Show()?

Here is the link to a demo. When I click on the ShowDialog() button I see the messagebox with the load and shown-event, but for Show() I only see the load Event.

Community
  • 1
  • 1
magicandre1981
  • 27,895
  • 5
  • 86
  • 127
  • 1
    You have to show us code — you know that. – LarsTech Jan 24 '17 at 17:04
  • @LarsTech what code? those 2 lines? to add an event handler and calling Show()? That makes no sense and causes closing as offtopic "why is my code not working" – magicandre1981 Jan 24 '17 at 17:08
  • 2
    Because there is something else going on. Post code that will duplicate the issue. – LarsTech Jan 24 '17 at 17:09
  • Unable to reproduce. I get both the `Load()` and `Shown()` events using `Show()` and `ShowDialog()`. Either you incorrectly wired up the events or something else is preventing those events from occurring. Do you have any third-party controls on the form? – Idle_Mind Jan 24 '17 at 17:57
  • @Idle_Mind and I see it all time. Post your small demo, I posted mine in a link. – magicandre1981 Jan 24 '17 at 18:49

1 Answers1

1
using (var form = new Form2())
{
  form.Show();
}

The Using block will dispose of your form immediately since Show will not block the code. ShowDialog will block the code until the form is closed, hence, that version works.

LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • I also tried it without 'using' and it still doesn't work. – magicandre1981 Jan 24 '17 at 19:07
  • @magicandre1981 Again, post that code in your question (not another demo project). When I remove the using block, I get the load and shown messages and the form appears – LarsTech Jan 24 '17 at 19:10
  • ok, I called Dispose too fast which is the same like using. I need the send data to form1 that shown event is displayed and dispose the form after I get the feedback from form2. – magicandre1981 Jan 24 '17 at 19:13