16

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?

Bugs
  • 4,491
  • 9
  • 32
  • 41
chris.au
  • 1,088
  • 2
  • 15
  • 41

19 Answers19

28

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).

Community
  • 1
  • 1
Marko Juvančič
  • 5,792
  • 1
  • 25
  • 41
  • 1
    If you can't get an exception, it may be because of this issue http://stackoverflow.com/questions/4933958/vs2010-does-not-show-unhandled-exception-message-in-a-winforms-application-on-a in which case the quick fix is to run WITHOUT debugging and see what happens. – AndyClaw May 21 '14 at 16:45
  • 1
    I've run into this situation twice with the same cause. My object has a DateTime property set to DateTime.MinValue when I bind that property to a DateTimePicker it fails silently and doesn't run the Form Load event handler. The solution for me was to set the Property default to DateTimePicker.MinimumDateTime. The difference between the values is DateTime.MinValue = 01/01/0001 and DateTimePicker.MinimumDateTime = 1/1/1753 – James Jul 27 '15 at 21:52
  • This saved my day. Thanx – zidane Dec 15 '17 at 12:29
  • 1
    This saved me a lot of debugging. In my case, I had deleted a Setting value that was bound to a control. You gotta love Data Binding. I mean it, it's line 4536 of the TOS, '..the user, you, hereby agrees to love data binding, or else'. – krowe2 Jan 22 '19 at 16:40
7

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.

John Whiting
  • 71
  • 1
  • 2
4

OK I had the SAME problem (I think) and the clues here helped. It was databinding (sort of)

I had properties of some controls bound to settings and when I delete these settings the form load event stopped running. Removed the bindings and now it is running again.

  • This just happened to me in Visual Studio 2015. In my case, I had changed the name of the application setting that the control was bound to, and even though the designer indicated that it had refactored the binding successfully, my Load event wasn't firing. Changing the binding to a random other application setting and then back again fixed it for me. – Sam Skuce Apr 07 '17 at 16:53
2

Here is another idea.

What happens if you set all exception types (not just for the CLR) to be thrown instead of user-unhandled. Does the application break anywhere at all?

Also, just to double check, you are in debug mode right?

Matt
  • 14,353
  • 5
  • 53
  • 65
1

Had the same problem. Checked my data bindings, everything looked ok. Got to thinking, even though form was closed, maybe .NET wasn't sure (old days, sometimes forms were only hidden and not really closed).

I added the event handler FormClosed and put a single line in it:

Private Sub frmScheduleInquiry_FormClosed(sender As Object, e As   System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
    Me.Dispose()
End Sub

Problem solved!

ccampj
  • 665
  • 1
  • 8
  • 18
1

Solved....

Have spent 4 hours and finally got clues from this answers. in my case i was having couple of TextBox control on the form bound to a BindingSource with respective column, i still have that bindingsource on the form but what was happened that i deleted one column from underlying database table so on the form there is still one TextBox exist pointing to that column with bindingsource, in fact there is no column like that because i deleted !..... this lead Form.load event was not firing ........finally fixed..

thanks to all of you ..

shab
  • 127
  • 9
1

I had the exact same problem just happen to me. Turns out I had added some ApplicationsSettings properties to a form TextBox control, but later wanted to delete them. I thought I had cleared everything out, but obviously I didn't - and this was what caused the Form_Load() (and maybe other events as well) to not fire. Deleting and then re-adding the offending TextBox did the trick.

Hope this helps

user3765883
  • 233
  • 1
  • 9
  • YES. Thank you. After removing a setting, I searched for `My.MySettings` in all the solution to remove it from various designer files. Now it fires those precious Load events. – Ivan Ferrer Villa Jun 12 '17 at 09:56
1

The problem you are experiencing may be caused by the application needing to fully load the form before you can do the "other code." This could be due to the other code dealing with objects on the form that haven't finished loading. You could use a timer that gets enabled in the load function to execute the other code. This way you don't have any timing issues and you can first load the form, and then a split second later, run the code you want from the timer.

Matt
  • 14,353
  • 5
  • 53
  • 65
  • this appears to be the closest thing that I can imagine that is happening... in fact if I call form.show, followed by form.hide and then form.show again the Load event is firing... I find this extremely strange behavior and would like to get to the source of the problem. – chris.au Nov 09 '10 at 03:07
1

Is your windows form inheriting from a base page? If so, the base page probably also has a Form Load event handler. In that base page Form Load event handler you will probably find an exception that is being thrown. So it is exiting the base page form load event handler and not firing the form load event handler in your inherited windows form.

Bubba
  • 41
  • 2
1

I had a similar issue, the problem was a mistake in the databinding. Omit the code for databinding and give it a try. I think the load event handler will be hit. Then see what's wrong with the databinding part.

Amr
  • 11
  • 2
0

I had a similar issue. It turned out that I was not using the Show method on the form - instead using a user32.dll ShowWindow call. This means the form still appeared, but the Load event was never fired because the dotNet Show method was never called.

Nick Wallace
  • 97
  • 1
  • 2
0

I know that this is an old post, but I thought that if someone was searching this issue, that my fix to this problem might help.

I was having this same problem as stated in the originally posted question, but I didn't have any data bound fields on the form. I found that the problem was in the fact that I was using the CurrentDeployment.CurrentVersion method and it was causing the silent problem. I set the application from debug mode to release and the problem still existed. Through trial and error I remarked out the defining method Dim xVersion As Version = ApplicationDeployment.CurrentDeployment.CurrentVersion and presto... its now working as usual.

I ended up changing the orginal code the code below.

    Dim xVersion As Version = ApplicationDeployment.CurrentDeployment.CurrentVersion
    sysVersion = String.Format("{0}.{1}{2}.{3}", xVersion.Major, xVersion.Minor, xVersion.Build, xVersion.Revision)

New code

#If (DEBUG) Then
    sysVersion = "[Debug mode]"
#Else
    Dim xVersion As Version = ApplicationDeployment.CurrentDeployment.CurrentVersion
    sysVersion = String.Format("{0}.{1}{2}.{3}", xVersion.Major, xVersion.Minor, xVersion.Build, xVersion.Revision)
#End If

I hope this helps someone. Happy Coding...

Lynn
  • 3,534
  • 5
  • 18
  • 18
0

Same problem, I rewrote the designer and that fixed it. The designer was loading then crashing (silently of course), and form_load was not firing due to that.

Rob
  • 2,363
  • 7
  • 36
  • 54
0

Matt is probably right about this one. Have you tried adjusting your code like this:

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
Else
      MessageBox.Show("argA has not been set")
End If
End Sub

If the messagebox appears it means that the event is fired before your argument is initialized. It would also account for the 'strange' behaviour concerning closing/opening the form.

Have you tried running the argA operations in the 'Shown' event?

Geoffrey
  • 956
  • 1
  • 10
  • 16
0

Had same issue, but cause was totally different. Form was being shown using form.Show() instead of form.ShowDialog()

syonip
  • 2,762
  • 25
  • 27
0

I experienced this symptom when building and running a .NET 4.0 WinForms application which loaded a series of older assemblies (.NET 2.0; .NET 1.1).

In the past I had seen this cause a mixed-mode assembly exception. In this particular case, the main Form loaded without exception and without executing any of its Form Load code.

The solution, in my case, was to set useLegacyV2RuntimeActivationPolicy="true" in the App.config document.

<startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
El-Ahrairah
  • 153
  • 1
  • 7
0

Make sure your "Solution Configuration" box at the top of your IDE is showing "Debug". I found that if it showed "Release" the "Load" method was not captured by the debugger.

Chris Raisin
  • 384
  • 3
  • 7
0

In my case, the main form had WindowState: Maximized set in the designer. This was causing the window to be drawn on screen prior to the Load event firing.

amonroejj
  • 573
  • 4
  • 16
0

Not sure if this will help, but I just ran into this issue because somebody mistakenly deleted the Handles Me.Load. I see you show MyBase.Load try changing it to Me.Load.

Hans
  • 1