Solved

VS 2008 VB.NET :: Sub Main()

Posted on 2008-10-01
7
4,497 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 86

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 86

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 Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

738 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