[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 507
  • Last Modified:

VB.NET application questions on Form_Load

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
maredzki
Asked:
maredzki
2 Solutions
 
Paul JacksonCommented:
try Application.Exit()
0
 
maredzkiAuthor Commented:
Application.Exit() did not work either.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
ZhaolaiCommented:
Have you tried "End" to terminate the program?
0
 
maredzkiAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
"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
 
maredzkiAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
(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
 
maredzkiAuthor Commented:
Thank you for the help, much appreciated!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now