Solved

VS 2008 VB.NET :: Sub Main()

Posted on 2008-10-01
7
4,492 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

831 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