Solved

VB.NET application questions on Form_Load

Posted on 2011-03-16
9
456 Views
Last Modified: 2012-06-27
I am trying to write a program that eventually will be running with no form present. Currently I have a form with few buttons for testing. I am not a vb.net programmer so looking for some insights.

I currently have the majority of the code in Form_Load and have an IF statement which should stop the execution of the program and close it. I tried multiple options, .Close(), .Exit(), .Dispose() with Me. or Form. which is executed but then continues on the next line.

I've tried calling a function within Form_Load and then including the Exit code but that did not work either.

Also tried Form_Paint function but that also just destroys the Form, but does not stop execution of the program.

The end-goal is to have the program execute on just double-clicking on it, no forms present and if specific condition arises, stop and close the execution.

Please be as specific as possible.

I hope this makes sense.

Thanks,
Marek
0
Comment
Question by:maredzki
9 Comments
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 35149710
try Application.Exit()
0
 
LVL 2

Author Comment

by:maredzki
ID: 35149869
Application.Exit() did not work either.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 35149950
See "Overview of the Visual Basic Application Model":
http://msdn.microsoft.com/en-us/library/w3xx6ewx(VS.90).aspx
http://msdn.microsoft.com/en-us/library/t4zch4d2(VS.90).aspx

Instead of using the Load() event, put your code into the Application.Startup() event.  To make the app close without showing the form at all, set "e.Cancel" to True.

Click on Project --> Properties.
Click on "View Application Events" in the bottom right.
Across the top of the code editor:
Change the Left dropdown from "(General)" to "(MyApplication Events)".
Change the Right dropdown from "(Declarations)" to "Startup".

You should have this in your editor with the blank method stub:  
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

        End Sub

    End Class

End Namespace

Open in new window


Now put your code in and place "e.Cancel = True" at the bottom to shut the app down:  
Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup

            ' ... put your code in here ...

            e.Cancel = True ' <-- Shut down app WITHOUT ever showing a form
        End Sub

Open in new window

0
 
LVL 17

Expert Comment

by:Zhaolai
ID: 35150188
Have you tried "End" to terminate the program?
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 2

Author Comment

by:maredzki
ID: 35150476
Idle_Mind,
cheers for the input.

How would you terminate the execution of the code inside of the startup event, tried the Close and Exit?

Thank you.
Marek
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 35150542
"How would you terminate the execution of the code inside of the startup event"

It's all spelled out in my post!...set "e.Cancel = True".  Afterwards you can call "Exit Sub" from the middle of code if you need an immediate drop out.
0
 
LVL 2

Assisted Solution

by:maredzki
maredzki earned 0 total points
ID: 35150944
Cheers for the help... indeed I needed the immediate termination of the code and 'Exit Sub' was not in your original post, so please don't get frustrated.

If I understand your post the Private Sub MyApplication_Startup would look like this then:

Dim num As Integer
num = 1
If num = 1 Then
     e.Cancel = True
     MessageBox.Show("this is an error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
     Exit Sub
End If

num = 0

If num = 0 Then
     e.Cancel = True
     MessageBox.Show("this is an error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
     Exit Sub
End If

Open in new window


Only the first If/End If statement (for num = 1) would get executed based on this rudimentary logic and exit the Sub.

Good to know that if you set e.Cancel = False, you should be able to get Form_Load going as well.

Thanks,
Marek


0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 35151087
(not frustrated here at all...sorry if it appeared that way)

That should work.

Right...the default for "e.Cancel" is False so if you don't explicitly set it then execution continues on as normal and the "Startup Object" specified in Project --> Properties gets loaded as normal.

On a side note, from within the Startup() event, you can also change the startup object by setting Application.MainForm to whatever instance you desire:
http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.applicationservices.windowsformsapplicationbase.mainform.aspx

    Application.MainForm = New SomeFormNameHere
0
 
LVL 2

Author Closing Comment

by:maredzki
ID: 35178826
Thank you for the help, much appreciated!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now