Results 1 to 7 of 7

Thread: form shown event not firing

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2015
    Location
    Connecticut, USA
    Posts
    147

    form shown event not firing

    I have a windows form project and would like to put some stuff in the Shown event handler instead of Load. I've done this before, you go to properties events and double click on Shown. The IDE generates the event handler for me. OK so far, but the event never fires. I've put a breakpoint in there and it never gets triggered. Is there something else that needs to be there?

    Visual Studio 2013.

    Thanks

  2. #2
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: form shown event not firing

    If it's C#:

    Open the .designer.cs file for the appropriate Form and see if there is a line that associates the Shown event with your handler. It'd look like:
    Code:
    this.Shown += My_Shown_Handler;
    If it's VB:

    Is there a "Handles" clause as part of your handler's declaration?
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: form shown event not firing

    If you go back to the Properties window, does it show your method next to the Shown event? If not, try selecting it from the drop-down. That should automatically generate the code that Sitten Spynne showed that you can add manually. Of course, that should have been generated when you double-clicked the event in the first place but VS, like any other software, doesn't always behave as it is supposed to.

  4. #4
    Junior Member
    Join Date
    Oct 2013
    Location
    California
    Posts
    16

    Re: form shown event not firing

    I was having this same problem and found it was due to my form being an MDI child form. when I removed the MDI child status, the form went through the Shown event code.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: form shown event not firing

    Quote Originally Posted by Greydog1 View Post
    I was having this same problem and found it was due to my form being an MDI child form. when I removed the MDI child status, the form went through the Shown event code.
    I just tested that in VS 2017 and I saw no such behaviour. This code:
    csharp Code:
    1. private void button1_Click(object sender, EventArgs e)
    2. {
    3.     var f2 = new Form2();
    4.     var f3 = new Form3 {MdiParent = this};
    5.  
    6.     f2.Show();
    7.     f3.Show();
    8. }
    saw the Load and Shown event handlers of both forms executed. Not sure if that's an issue that has been fixed or what but it seems OK to me.

  6. #6
    Junior Member
    Join Date
    Oct 2013
    Location
    California
    Posts
    16

    Re: form shown event not firing

    Well that probably explains why I can't seem to find ANY information about MDI child forms not triggering the Shown event. However, I am still upgrading a VB6 project so am using VS2008 and also noticed this is a C# post and I am using VB. However, I can change the behavior simply by commenting out the MDIparent assignment statement in my environment.

  7. #7
    Junior Member
    Join Date
    Oct 2013
    Location
    California
    Posts
    16

    Re: form shown event not firing

    Thanks for pointing that out jmcilhinney. I tried this on a different form that is also an MDI child form and the Shown event executed as expected. So I guess I have some other trail to follow with the first form. Thanks again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width