Solved

VB.NET application questions on Form_Load

Posted on 2011-03-16
9
463 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 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

16 Experts available now in Live!

Get 1:1 Help Now