Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Form.Load event not firing, form showing

I fear that there is something obviously wrong with my code, but I have come across a situation where the Form.Load event is not firing when I create and show my form.

The form is not subclassed (as I've seen some problems with that in some searches), and I am not getting any errors thrown when I step through the code in the debugger.

I have a break point set on the IDE-created form load function (which does have the Handles MyBase.Load signature suffix) but the breakpoint is never reached and the form does display and work.

The form is passed three arguments in the constructor but the IntializeComponent() function is called before anything else is done.

Code:

Public Sub New(ByVal argA As Object, ByVal argB As Object, ByVal mode As FormMode)

    ' This call is required by the Windows Form Designer.
    InitializeComponent()

    ' Other code here,
    ' No errors generated
    '

End Sub

The form load function is as follows, (but this is never actually executed as the event is not fired).

Code:

Private Sub frmInstrumentEditor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If Not argA Is Nothing Then ' argA set in constructor
          ' Operations using argA
    End If
End Sub

I might add I am using some databinding with some controls and the argA object, but if this was producing an error I thought I would have seen this (I have CLR Execpetions settings set to Thown in the debugger > exceptions window)

Any ideas why this might be occurring?

like image 778
ChrisAU Avatar asked Nov 09 '10 02:11

ChrisAU


People also ask

How do I refresh winform?

You can use the Form. Invalidate(); or Form. Refresh(); methods.

What is form load event?

The Form Load Event in VB . NET. An important event you'll want to write code for is the Form Load event. You might want to, for example, set the Enabled property of a control to False when a form loads. Or maybe blank out an item on your menu.

What is event in Windows form?

An event is an action that you can respond to, or "handle," in code. Events can be generated by a user action, such as clicking the mouse or pressing a key, by program code, or by the system.


2 Answers

I just had a similar issue (it was in Shown event, not Load, but the root cause is the same). The reason was hidden deep in one of the ancestors - there was an unhandled NullReferenceException thrown and this exception was somehow "muted".

I found it after extensive debugging with F11.

But... when writing this answer I found this post on SO

Just add Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException) in your Main() method.

If you're using a 64-bit machine, it provides you with the solution (it worked in my case, too).

like image 56
Marko Juvančič Avatar answered Oct 26 '22 16:10

Marko Juvančič


I had a similar problem. On opening the form for the first time, the load event would not be tiggered but on opening it the second time, all would be well. The problem tuned out to be one of my text boxes which was bound to a field that I had deleted from the database (sql server - I was using datasets, tableadaptors and bindingsources in a fairly standard way).

Make sure that all the controls on your form that are databound have fields that exist in the dataset and that the dataset is an accurate reflection of the underlying database table (the easiest was to do this last bit is to use the "Configure data source with wizzard" button on the data sources window (menu -data - show data sources) and remove the table. Then use it again to add the table back- this should make sure all the data matches.

Hope this helps.

like image 35
John Whiting Avatar answered Oct 26 '22 17:10

John Whiting