Solved

VB.NET application questions on Form_Load

Posted on 2011-03-16
9
473 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 17

Expert Comment

by:Zhaolai
ID: 35150188
Have you tried "End" to terminate the program?
0
 
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

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

808 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