Answered by:
VB 1st Run Procedure Order? Transitioning from VBA to VB

Question
-
I am coming from VBA to VB, so by explaining what I did in VBA, I hope that someone who knows both languages will better understand what I'm trying to do.
I want to create a public array and set some values for the contents. I can't figure out how to create a procedure that I'm sure is executed before anything else is done. In VBA I could put this in an AutoOpen or AutoActivate procedure in a Module.
1. Is there any equivalent to AutoOpen, AutoActivate, AutoClose etc. in VB?
2. It seems that procedures in the Form get fired before those in the Module, which is the exact opposite of VBA. Is this correct?
3. Is/Are there any good article(s) on transitioning from VBA to VB that you could point me to?'
Thanks,
Stu the K.
Wednesday, January 2, 2013 11:26 AM
Answers
-
Hi
Not sure if this is what you want.
You can make subroutine calls from the Form load event which would call these before anything else.
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load MySub1() MySub2() ' continue with other code End Sub
Regards Les, Livingston, Scotland
- Proposed as answer by Blackwood Wednesday, January 2, 2013 11:45 PM
- Marked as answer by Youen ZenModerator Thursday, January 10, 2013 8:38 AM
Wednesday, January 2, 2013 2:19 PM -
I agree with Les. Assuming this is a Windows Forms application, the usual place to initialise data is in the Load Event of the Startup Form (which by default is called Form1). There are places to but code that will be executed earlier, but you probably don't need to worry about them.
And note that, although your post referred to "the" Form and "the" Module, a typical application consists of one or more Forms, several Classes and few, if any, Modules (some people would discourage the use of Modules saying that they are not in line with Object Oriented principles). One of the Forms will be designated as the Startup Form, and this is the one whose Load event can be used to initialise data.
- Edited by Blackwood Wednesday, January 2, 2013 11:56 PM
- Marked as answer by Youen ZenModerator Thursday, January 10, 2013 8:38 AM
Wednesday, January 2, 2013 11:50 PM -
Stu,
It seems, VB for Visual Studio Net has no startup module, but it has but in many cases that is hidden or even implemented in the application framework. However it is there.
That does also the Open, activate and close of the program.
The module contains then in the most simple way in a Windows Forms project this code, but and you can make it yourself.
Application.EnableVisualStyles() Application.SetCompatibleTextRenderingDefault(false) Application.Run(new Form1())
What you see it opens (runs the first Form and if that form closes it comes back in the module which then has nothing more to do and ends).
I hope this clears it a little bit more for you.
Success
Cor- Edited by Cor Ligthert Thursday, January 3, 2013 12:09 PM
Thursday, January 3, 2013 12:08 PM -
Thank you both for your replies. I was using a Module as that is what gets fired first in VBA. I can/have eliminated that.
I want to set some flags to track if something is changed so I know to ask if they want to Save before exiting. I do that with Boolean variables vChanged and vSaved. The both are to be set to False. Also I have a some items that it is easier for me to create an array to hold them then to loop though them to set their values.
This is a fairly simple program and it only has 1 form. I tried the form's Load and Activate procedures but I'm finding that the changed sub-routines for controls on the form are being fired off before either the form's Load or Activate sub-routine is run. Any idea why that is happening?
I found the form's New() routine. The auto-code for that said to put my initializing code after InitializeComponent() but that didn't work as the Changed event for the controls were still firing before my initializing code. I found that I can do what I want by putting it before the InitializeComponent() call.
Stuart
- Marked as answer by Youen ZenModerator Thursday, January 10, 2013 8:38 AM
Saturday, January 5, 2013 6:58 PM
All replies
-
Hi
Not sure if this is what you want.
You can make subroutine calls from the Form load event which would call these before anything else.
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load MySub1() MySub2() ' continue with other code End Sub
Regards Les, Livingston, Scotland
- Proposed as answer by Blackwood Wednesday, January 2, 2013 11:45 PM
- Marked as answer by Youen ZenModerator Thursday, January 10, 2013 8:38 AM
Wednesday, January 2, 2013 2:19 PM -
I agree with Les. Assuming this is a Windows Forms application, the usual place to initialise data is in the Load Event of the Startup Form (which by default is called Form1). There are places to but code that will be executed earlier, but you probably don't need to worry about them.
And note that, although your post referred to "the" Form and "the" Module, a typical application consists of one or more Forms, several Classes and few, if any, Modules (some people would discourage the use of Modules saying that they are not in line with Object Oriented principles). One of the Forms will be designated as the Startup Form, and this is the one whose Load event can be used to initialise data.
- Edited by Blackwood Wednesday, January 2, 2013 11:56 PM
- Marked as answer by Youen ZenModerator Thursday, January 10, 2013 8:38 AM
Wednesday, January 2, 2013 11:50 PM -
Stu,
It seems, VB for Visual Studio Net has no startup module, but it has but in many cases that is hidden or even implemented in the application framework. However it is there.
That does also the Open, activate and close of the program.
The module contains then in the most simple way in a Windows Forms project this code, but and you can make it yourself.
Application.EnableVisualStyles() Application.SetCompatibleTextRenderingDefault(false) Application.Run(new Form1())
What you see it opens (runs the first Form and if that form closes it comes back in the module which then has nothing more to do and ends).
I hope this clears it a little bit more for you.
Success
Cor- Edited by Cor Ligthert Thursday, January 3, 2013 12:09 PM
Thursday, January 3, 2013 12:08 PM -
Thank you both for your replies. I was using a Module as that is what gets fired first in VBA. I can/have eliminated that.
I want to set some flags to track if something is changed so I know to ask if they want to Save before exiting. I do that with Boolean variables vChanged and vSaved. The both are to be set to False. Also I have a some items that it is easier for me to create an array to hold them then to loop though them to set their values.
This is a fairly simple program and it only has 1 form. I tried the form's Load and Activate procedures but I'm finding that the changed sub-routines for controls on the form are being fired off before either the form's Load or Activate sub-routine is run. Any idea why that is happening?
I found the form's New() routine. The auto-code for that said to put my initializing code after InitializeComponent() but that didn't work as the Changed event for the controls were still firing before my initializing code. I found that I can do what I want by putting it before the InitializeComponent() call.
Stuart
- Marked as answer by Youen ZenModerator Thursday, January 10, 2013 8:38 AM
Saturday, January 5, 2013 6:58 PM