How to properly close a windows application in vb.net - vb.net

I have a windows application that when I close it still runs on the background process of task manager. I tried going through Application/Shutdown Mode/When start up form closes, but my startup form is a login form which then takes me to my main application which has a close button. I also tried the option When last form closes, no effect.
On this close button I have tried Me.Close(). and also I have tried Application.Quit() my both still not closing my application completely.
How can I terminate the application completely and clear it from the background services?

"my startup form is a login form which then takes me to my main application"
You are probably hiding the login form with Hide(), which is keeping the app alive. With the When last form closes option set, you can instead call Close() against the login form after showing the main form. Then it should shut down properly.

use Application.Exit or End...

In Login form
create a instance of MainForm
show the MainForm and close the Login form
If you want to pass any value to MainForm from Login form, create a public variable in MainForm. Eg. Public userAuth As String
Example:
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
Dim mf As New MainForm()
mf.userAuth = "Admin"
mf.Show()
Me.Close()
End Sub

Related

VB - Form 2 closes automatically whenever Form 1 closes?

So right now, I'm so frustrated because there is ONE little problem that stops me from continue working on my programm.
I made a simple Login Form where the user must type in a password and when he failed after 3 attempts, the form will close. BUT if the user enters a correct password, a button will become visible and he can Login and another Form with the main programm will open. So I want to close the Login Form when the user presses the Login button and the Form 2 will appear. Simple Code:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
logint = 1
Form2.Show()
Me.Close()
logint handels the attempt counter... WHATEVER.. the MAIN PROBLEM is, that whenever I press Login the Login closes instantly and the form 2 pops up, stays for like 2 Seconds and closes right after that. I searched both codes and there is no "Form2.Close() " or "Me.Close()" which could cause that error. Form 2 always exits with Code 0 (0x0). What can I do if I want just the Login Form closed and the Main form loaded?
If you right click on your project and select the Propeties voice from the Popup menu and switch to the Application page you will notice a combobox that says:
Shutdown mode = When Startup Form close.
Your startup form is the Login form, so when it closes your app terminates.
You can change this settings to
Shutdown mode = When last form closes.
However, pay attention to not have orphaned forms around your application otherwise it will never stop.
A better approach is to start your application with the normal form and in its constructor execute the login form. If the login form doesn't allow your application to continue, set a global variable and Close the main form in its Load event.

Progress bar value events not reacting to value

I have used a timer, a progress bar and incremented the value by 2 on each timer tick. After that, "Main" should load, but it doesn't. Why? I did use a tracepoint which said the value was 100 and then the program exited with code 0.
Public Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
ProgressBar1.Increment(2)
If ProgressBar1.Value = 100 Then
Main.Show()
Me.Close()
End If
End Sub
Presumably you are using the Application Framework and you have selected the form that has that timer as the Startup object and you have the Shutdown mode set to the When startup form closes option (these are all options on the Application tab of the project properties designer window). I suspect that is the case because those are the default settings for a new VB WinForm application project. When you have your project configured that way, the application will automatically close when the startup form closes. Therefore, when you call Me.Close() on the first form, the application terminates immediately, even when it has already shown another form. One way to correct it is to change the Shutdown mode to the When last form closes option.

VB using me.close before opening new form

Hi Guys i'm new to Visual Basic Coding, and i can't seem to get where's my mistake on my coding, i'm trying to create a button that opens a new form while closing the current form.
i have two forms, form 1 is MainForm, form 2 is SearchForm
Whenever i use this code:
Private Sub SearchMButton_Click(sender As Object, e As EventArgs) Handles SearchMButton.Click
MainForm.Close()
SearchForm.Show()
End Sub
End Class
it will generate an error and says i need to replace MainForm.Close() into Me.Close()
When i Use this
Private Sub SearchMButton_Click(sender As Object, e As EventArgs) Handles SearchMButton.Click
Me.Close()
SearchForm.Show()
End Sub
End Class
It closes both Forms and it doesn't leave any Form Open. Kindly direct me to the proper path, thanks in advance.
You need to Hide the form rather than closing it. Since it's your main form, when it closes, the application exits.
Standard UI guidelines are to leave the main form open, and open search form on top of that. If you need to block the main form, while search criteria are selected, use .ShowDialog, instead of just .Show.
.NET WinForms programming pattern kind of implies that you never close your main form. If you deviate from this approach, you are guaranteed to encounter all sorts of layout and display issues. So please don't. You can .Hide the main form, if it needs to go to system tray or run in background.

close form when application loses focus

My form is displayed as TopMost on my application. The problem I have is that whenever I minimize my application or it loses focus, the form remains displaying. I want to be able to minimize my application or move to another and also hide or close my form. Once the application regains the focus, then unhide or open the form again.
Here is what I worked out on the form's closing event:
Private Sub frmNavigation_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Static Minimize As Boolean
If Minimize = True Then
e.Cancel = True
Me.Hide()
End If
End Sub
I tried using the same code in the applications WindowDeactivate event but nothing happens.
You do not show how you create the instance of your frmNavigation. I am assuming that you are using the Show Method, so just use the version of Show that you pass in the top level window. That will assign the owner of the form, it will then stay on top of your Main Form and minimize and restore with it also. If this doesn't work please show how you are creating and showing your form.
frmNavigation.Show(Me)
I was able to find an answer to the question. MSDN had an article on this very issue.
it can be found here: http://support.microsoft.com/kb/186908#appliesto

close windows form when users click outside

anybody have an idea how to write a code if the user clicks outside windows form, the form will automatically close? I.e., I had two forms, when i show form2 and then i click outside of it, form 2 will closed.
Does this help? Try the first two solutions, one of which should work for you.
Winforms: Close modal dialog when clicking outside the dialog
You can simply utilize the LostFocus built-in event of the Form like this
Private Sub Form1_LostFocus(ByVal sender As Object, ByVal e As EventArgs) _
Handles Me.LostFocus
Me.Close()
End Sub