0

I found something interesting about form_load event that I could not understand. In my opinion, everything in the form_load will be executed first, however, it is not the case.

When I put

Msgbox("form1_load is being called")

in the first line of Form1_load event, the message box will be popped out.

If I put many staffs before Msgbox method, say, reading from the serial port, initializing checkbox, Msgbox will not be popped out.

The reason this bothers me is because I have initialized my checkbox in the form_load, however, when I tried to access checkbox object in my code somewhere else, the compiler complains that the checkbox object is null. The way I get around it is to initialize checkbox object right before I use (access) it.

What happens behind this? Does it mean Form_load does not guarantee to execute all the code first?

Thanks.

success
  • 75
  • 2
  • 11
  • 1
    There is an oddball bug in that if you encounter an exception in FormLoad, it isnt reported - NET just gives up and shows the form, skipping any other code. If you step thru the code in debug you can see it exit prematurely (and therefore know which line at least is at fault). – Ňɏssa Pøngjǣrdenlarp Aug 02 '16 at 17:32
  • The bug is explained here: http://stackoverflow.com/a/4934010/1070452 – Ňɏssa Pøngjǣrdenlarp Aug 02 '16 at 17:35
  • I guess the safe for me is to initialize the object right before I access to it. The weird thing is that my program worked fine when I initialized my checkbox object in the form_load yesterday morning but failed at yesterday evening. – success Aug 02 '16 at 18:02
  • 2
    Well, it means that some portion of your code is not executing at all, and there is some exception being thrown. The best thing to do is to find and fix the problem. The link tells how to force it to report so you can find it. – Ňɏssa Pøngjǣrdenlarp Aug 02 '16 at 18:03
  • Oh I see. I better fix it. Thanks again Plutonix – success Aug 02 '16 at 20:00

0 Answers0