How to find where form code is entered?

teleute

Member
Joined
May 7, 2013
Messages
7
Programming Experience
10+
My apologies for this very likely stupid question - I'm brand new to VB .NET (and pretty new to OO programming and VS in general - I've usually been a scripter).

I have a VB .NET application with a main form. In main.vb there's a subroutine called frmMain_Load , which one presumes loads that main form. I'm trying to find what code if any gets executed before that subroutine. However, if I right-click on it and say "Find all references", it finds nothing, which puzzles me. If I put a breakpoint there it hits it, so that code is definitely being executed, but for the life of me I can't figure out what calls it, or how to find the very beginning of the code.

I think I've seen something about a code recording function in newer versions of VS, but we're on 2008 and it doesn't seem to exist.

Anyone who can shed some light on this for me? Thank you!
 
Hmmm...I just found the step into functionality, which partially answered my question (but brought up a couple more!). It seems to start in a file called ApplicationEvents.vb, with a Sub called MyApplicationStartup. This seems to be a standard thing for VB .NET, in place of the concept of it starting in "main", like in C and similar languages. (Correct?)

What I still find non-intuitive is that there's no visible call to the frmMain_Load subroutine. From what I can tell, the code knows to do this because frmMain is set as the "Startup form" in the project properties. Does that seem correct as well?

This is really odd to me, and taking a while to get my head around. To me, if there's code executing, I should be able to find the code that's telling it to execute...:-/
 
Last edited:
Load event handlers is called during the load sequence of the Form class by its internal code raising that event.

A form is a class, so the first method that is executed when an instance of it is created is its constructor (Sub New). If a constructor is not defined VS generates one when project is compiled, it includes a call to InitializeComponent method. InitializeComponent method is part of the designer generated code, where controls/components is initialized according to your configurations in designer. To see the designer generated code use Solution Explorer and click 'show all files' icon and expand the class node, there you will see a theform.Designer.vb file with that content.

Before main form is created application starts up from its Main method (entry point), this is included by default by application framework code. If you want to add application startup code handle application Startup event.
 
What I still find non-intuitive is that there's no visible call to the frmMain_Load subroutine. From what I can tell, the code knows to do this because frmMain is set as the "Startup form" in the project properties. Does that seem correct as well?
Documentation for Form class has this short description about Load event:
help said:
Occurs before a form is displayed for the first time.
So if you want to add code to be executed at that time, then that is the event you would want to add event handler for.
This is really odd to me, and taking a while to get my head around. To me, if there's code executing, I should be able to find the code that's telling it to execute...:-/
The thing about events is that only the class that defines an event can raise it, and when it does all event handlers that is added (either by WithEvents/Handles or AddHandler) is called in turn. What you're dealing with here is really inheritance, the form you design in VS is a class that inherits the .Net Framework Form class defined in a library (dll), that class already contains large amounts of code. According to standard coding conventions for events there is a separate Protected Overridable method that raises the event, for Load event this method is called Onload. In documentation this short description appears:
help said:
Raises the Load event.
Your derived class can override this method, if you do you will see a call to the base OnLoad method and can add code before or after that call to have things happen before or after the event. Normally though, in a inherited classes in an application you would just handle the event by adding an event handler method.
 
The thing about events is that only the class that defines an event can raise it, and when it does all event handlers that is added (either by WithEvents/Handles or AddHandler) is called in turn. What you're dealing with here is really inheritance, the form you design in VS is a class that inherits the .Net Framework Form class defined in a library (dll), that class already contains large amounts of code. According to standard coding conventions for events there is a separate Protected Overridable method that raises the event, for Load event this method is called Onload.

Okay - I think I'm wrapping my head around that. Thanks! (Oh the joys of transitioning to OO from a lifetime of scripting...:) )
 
Back
Top