Solved

VS 2008 VB.NET :: Sub Main()

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

777 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