Solved

VB.NET application questions on Form_Load

Posted on 2011-03-16
9
479 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
[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
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 86

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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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 86

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 86

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 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