Solved

VS 2008 VB.NET :: Sub Main()

Posted on 2008-10-01
7
4,481 Views
Last Modified: 2013-11-26
Dear Experts,

If I would like to execute some code on a form's creation or on create event I have read I need to use the Sub Main() procedure.

I have build a demo app and in Sub Main() I did the typical "Hello Word" messagebox but it do not display when running the application.

What am I doing wrong?
Please see code:

Public Class frmMain

    Shared Sub Main()

        MessageBox.Show("Hi")

    End Sub
 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerateLabels.Click

        Dim frmGenerate As New frmGenerateLabels

        frmGenerate.Show()

    End Sub
 
 

End Class

Open in new window

0
Comment
Question by:Marius0188
  • 3
  • 2
7 Comments
 
LVL 11

Expert Comment

by:ladarling
ID: 22615986
First, you need a new module file:

Module Startup
Public Sub Main()
Dim myForm As New Form1
MsgBox("hello world")
Windows.Forms.Application.Run(myForm)  
End Sub
End Module
Then you have to disable the App framework in your projects properties to be able to start the project from sub main. Once Main() takes charge, you can start the app framework, modify forms, whatever...
 
0
 
LVL 11

Expert Comment

by:ladarling
ID: 22616011
Here is how your properties should look:
screenshot.PNG
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22616161
The author said:

    "If I would like to execute some code on a form's creation or on create event I have read I need to use the Sub Main() procedure."

If you are dealing with a FORM, then you want to add code to the CONSTRUCTOR of your Form...also known as the "New" event:

    Public Class frmMain

        Public Sub New()

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

            ' Add any initialization after the InitializeComponent() call.
            ' ...your code in here...
        End Sub

    End Class

If you want to do something when a Form is DISPLAYED (rather than created) then use either the Load() or Shown() events.

If you want to run something at the beginning of your APPLICATION (before any forms are displayed) then use the Application.Startup() event:
http://msdn.microsoft.com/en-us/library/w3xx6ewx.aspx
http://msdn.microsoft.com/en-us/library/t4zch4d2.aspx
0
 
LVL 11

Accepted Solution

by:
ladarling earned 250 total points
ID: 22616323
If I would like to execute some code on a form's creation or on create event I have read I need to use the Sub Main() procedure.
If you are dealing with a FORM, then you want to add code to the CONSTRUCTOR of your Form...also known as the "New" event:
I thought what he was getting at was how to execute code prior to form creation. Like Idle said, if you need to catch the forms constructor directly then, New() is the bunny.
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 250 total points
ID: 22616403
Also, I see ALOT of people using Sub Main when they really shouldn't be...

I suspect that they have found examples for VB.Net 2003 (or below) when the Application.Startup() event and the Application Model didn't even exist yet.  Many other languages use the concept of "Main" as well...Microsoft didn't do anyone any favors by BURYING access to the Application Events via a button in the very bottom right (not visible by default on my monitor!...have to scroll to find it!) of the Project --> Properties section on a specific Tab.

=\
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now