Solved

Getting VB.NET 2008 to go to Sub Main() on startup

Posted on 2011-02-22
12
1,212 Views
Last Modified: 2012-05-11
Hello Experts -

I'm trying to get VB.NET 2008 to goto Sub Main() instead of the startup form. The reason why is that the logic in Sub Main() will determinde which form is shown.

When I put Sub Main() in a Module, VB ignores it and goes to the default startup form.

Any thoughts?
0
Comment
Question by:gbmcneil
[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
  • 4
  • 4
  • 3
  • +1
12 Comments
 
LVL 8

Expert Comment

by:Toxacon
ID: 34953582
Use the form selection logic in the startup form. Hide the startup form before it's shown during form_load. You can also make a stylish start with a splash screen which has the main form selection logic in it.
0
 

Author Comment

by:gbmcneil
ID: 34953645
You are saying that I can't get VB.NET to go to my Sub Main() in a module?
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 34953745
You can do it IF you do not select to create a Windows Forms application.
Choose to create a class (and remember to include the system.windows.forms) when creating your forms.
0
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
LVL 8

Expert Comment

by:Toxacon
ID: 34953871
You can call the Main() as any other procedure but after finishing it will go to the startup form.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 34954051
You can...just go to Project --> Properties and UNCHECK the "Enable Application Framework" box.  Then you can change the "Startup Object" to "Sub Main"
0
 
LVL 8

Expert Comment

by:Toxacon
ID: 34954245
If you disable the Application Framework, you have to programmatically take care of visual styles, proper shutdown/form handling etc...
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 34954453
Re-reading the question, he has:

    "The reason why is that the logic in Sub Main() will determinde which form is shown."

To change the startup form, use the Application.Startup() event (Project --> Properties --> "View Application Events" button in the bottom right.)

From the Startup() event, you can put your logic in and change the Application.MainForm property to an instance of your choosing:
Namespace My

    ' The following events are available for MyApplication:
    ' 
    ' Startup: Raised when the application starts, before the startup form is created.
    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
    ' UnhandledException: Raised if the application encounters an unhandled exception.
    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 
    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
    Partial Friend Class MyApplication

        Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
            If Date.Today.DayOfWeek = DayOfWeek.Tuesday Then
                Application.MainForm = New Form1
            Else
                Application.MainForm = New Form2
            End If
        End Sub

    End Class


End Namespace

Open in new window

0
 

Author Comment

by:gbmcneil
ID: 34955291
That mind of yours is not very idle, Idle Mind. You've hit the nail on the head.

Your solution is exactly what I needed.

Thanks very much.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 34955316
Glad that was helpful.

Just FYI, you can also CANCEL the app from the Startup() Event by setting "e.Cancel = True",  giving you the ability to shutdown the app WITHOUT showing any forms at all.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 34955330
See "Overview of the Visual Basic Application Model":
http://msdn.microsoft.com/en-us/library/w3xx6ewx(VS.80).aspx
0
 

Author Comment

by:gbmcneil
ID: 34955769
Thanks again. Good info to know.
0
 

Author Closing Comment

by:gbmcneil
ID: 34988143
Thanks fot your help.
0

Featured Post

Containers and Docker for Everyone

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

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

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…
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…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

729 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