Results 1 to 3 of 3

Thread: Why Won't My Form_Load Executing?

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Why Won't My Form_Load Executing?

    I see the question asked: Why won't my form_load event fire? or Why isn't my form_load executing one way or another quite a bit and I felt the need to write an example on different solutions on how to solve this issue.

    The first and most likely culprit is that you're missing your handles clause. If you've cut/paste code from the internet or from existing code or perhaps you're brave and trying to type out the event free handed, it's likely that the handles clause may get removed by the IDE. How can you tell if the handles clause is missing? Well if you form_load event(or any other event for that matter) looks like this:
    Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Console.WriteLine("Foo")
    End Sub
    Then you're missing the handles clause. Here is how the form_load event should look like:
    Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Console.WriteLine("foo")
    End Sub
    Or
    Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
        Console.WriteLine("foo")
    End Sub
    If that solves your problem, don't worry as it happens to the best of us too! If it doesn't solve your problem, then read on.

    The second culprit can actually be a bug within the IDE. If you're running Windows 7, it is the x64 Bit system, and you have the Handles clause at the end of the event line; then it's more than likely this bug. What happens is if your code is to throw an exception in the form_load event, Windows will swallow that exception and not execute the remainder of the code in the event. Here is a good example of what would cause an exception in the form_load event and get swallowed by Windows:
    Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim sr As IO.StreamReader
    
        Console.WriteLine(sr.ReadLine())
        Console.WriteLine("Finished")
    End Sub
    What would happen normally, is that an exception would occur because the StreamReader has not been set to a new instance. But let's assume for a second that we do not know why an exception would normally occur, how would we find out what is causing an exception? Well there are three popular options.

    The first option is to move the code to the Form_Shown event. The Form_Shown event will not swallow the exception like the Form_Load would.

    The second option is to wrap the whole code into a Try/Catch statement and wait for the exception to occur.

    The third option is to set breakpoints on every line inside of the Form_Load event. You will know which line throws the exception as it will be the last line to trigger the break.

    After you follow one of those options, you will recognize that a NullReferanceException would be thrown because(as stated before) the StreamReader is not set to a new instance. In this particular case I'd adjust how I declare the StreamReader.

    These are the two most probable suspects. However, if you do have a unique situation where your form_load event will not execute; please let me know so that I may update this thread.
    Last edited by dday9; May 29th, 2014 at 01:08 PM. Reason: Made corrections based on TG's information in #2
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Why Won't My Form_Load Executing?

    What happens is if your code is to throw an exception in the form_load event, the compiler will swallow that exception and not execute the remainder of the code in the event.
    Almost true.. it's actually Windows that swallows the error... not the IDE... 1) if you add in a try catch, you can catch the exception when it happens, if the IDE was responsible for eating the error, that wouldn't work either. B) It's something the Windows group did so that it would prevent errors from cropping up when opening applications (this seems to indicate that it affects more than just .NET) at any rate the mythology is that there's a difference between the Windows group and the Dev tools group over how it should work.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Why Won't My Form_Load Executing?

    Thanks for that TG, I've updated my post!
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

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