Link to home
Start Free TrialLog in
Avatar of Marius0188
Marius0188

asked on

VS 2008 VB.NET :: Sub Main()

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

Avatar of ladarling
ladarling
Flag of United States of America image

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...
 
Here is how your properties should look:
screenshot.PNG
Avatar of Mike Tomlinson
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
ASKER CERTIFIED SOLUTION
Avatar of ladarling
ladarling
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial