Results 1 to 4 of 4

Thread: [RESOLVED] [2005] Form load event not firing as expected

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2007
    Posts
    9

    Resolved [RESOLVED] [2005] Form load event not firing as expected

    I'm having a problem with a form load event not firing when I create a new instance of a form. I'm sure I'm probably missing something really simple, but I just can't seem to see it. Please ignore the purpose of this project as it is merely a simple demonstration of behavior I have run into in a much more complex project.

    Setup: One form (frmTest) containing a text box (txtTest), a "New Form" button (cmdNewForm), and an "Exit" button (cmdExit).

    Code:
    Public Class frmTest

    Private Sub frmTest_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Call MsgBox("Loading...")
    End Sub

    Private Sub cmdNewForm_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdNewForm.Click
    Dim frmNew As New frmTest
    Call MsgBox("Test 1")
    frmNew.txtTest.Text = "Test"
    Call Me.Hide()
    Call MsgBox("Test 2")
    Call frmNew.ShowDialog()
    frmNew = Nothing
    Call Me.Show()
    End Sub

    Private Sub cmdExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdExit.Click
    Call Me.Close()
    End Sub

    End Class

    Expected behavior: Upon clicking the "New Form" button, a new instance of frmTest should be created, causing the frmTest.Load event to fire so that the msgbox pops up with "Loading...", followed by "Test 1" and "Test 2".

    Observed behavior: Clicking the "New Form" button appears to create a new instance ("Test" appears in the text box when shown), but the order of the msgboxes is "Test 1" then "Test 2" and finally "Loading..." Thus it appears that the form load event is not being triggered until ShowDialog() is called.

    Any suggestions? I tried removing the form load event and re-inserting it as that seemed to solve an issue in a different thread, but it didn't work for this.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Form load event not firing as expected

    That's how it works. The form's Load event doesn't get raised when the form is created. It gets created when the form is displayed for the first time, i.e. when you call Show or ShowDialog. Once the form's Load event handler has completed the form will become visible, regardless of whether you have previously called its Hide method or set its Visible property to False.

    If you want to do something when the form is created you do it in the constructor, but you'd never show a MessageBox from a form constructor.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2007
    Posts
    9

    Re: [2005] Form load event not firing as expected

    Thanks. I guess I'm a little too used to VB6 since that's what I've had training in, and with that you can explicitly load the form prior to showing it so I was expecting the same in .NET. I tried to find documentation along these lines, but for some reason I couldn't find anything. Also, I am well aware that I should be careful about when I show msgboxes; it just happened to be an easy way to test when things were happening.

  4. #4
    Fanatic Member
    Join Date
    Aug 2006
    Location
    In my head
    Posts
    913

    Re: [2005] Form load event not firing as expected

    You can create a Sub New() in your primary form and perform any setup you need to within it. Then when you create a new copy of that form, it will do what you ask it to do.
    Platforms of choice: Visual Studio 2005/2008 Professional : Visual Studio 2010 Enterprise : PHP - Notepad++/WAMP

    Please Rate If I helped you.
    Please remember to mark threads as closed if your issue has been resolved.

    Reserved Words in Access | Connection Strings

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