VB - Form 2 closes automatically whenever Form 1 closes? - vb.net

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.

Related

Visual Basic Opening A Form [duplicate]

I have a login form which I need to close without the entire application being terminated. I tried using Me.Close() , Me.Hide() as well. The login form is used as the main form as well.
I hope this makes sense....
It sounds like you have a VB.Net project and your Login form is your 'startup form'. When you close that form, your application thinks it is over; but you really want to take action after the Login form is closed.
If you bring up the Properties window for the project, on the Applications Tab you can set the 'Shutdown mode'. The default is when the 'Startup Form Closes'. Change it to 'When the last form closes'.
You can also add Application level events here.
http://msdn.microsoft.com/en-us/library/f2bys999(v=vs.80).aspx
If you stick with the way you are going; your Login form is going to have to create another form before it closes or your app will close. You can do that; but it's probably cleaner to move the login logic into the Application Startup Event (see link for more details).
In the startup event you can show the Login screen, get the result, decide if you want to show the main form for your application, etc, etc...
This depends on where you are trying to close or hide the form. If you are trying to close or hide the form from within the the form itself then Me.Close() and Me.Hide() does the job. If you are attempting to close or hide a form from another form like your main form then you must refer to the form instance example:
frmAbout.Close()
frmAbout.Hide()
I hope this helps.

Closing a Dialog Form and Open another form

I've got (what I believe to be) a weird issue with closing a form and opening another.
I have a button on a particular form (that is opened using the .ShowDialog if that makes any difference), when the button is clicked, the following code runs:
Me.Close()
LC.ShowDialog()
I would expect that the form containing the button should close and the LC form should open as a dialog form. What actually happens is that the Me form stays open and the LC form appears behind it with main focus.
Why would this be?
UPDATE 1
Just to clarify the set up of the forms:
Form1 opens the me form as a Dialog (Where Form1 is the main form that is launched on startup)
The me form opens the LC form and should close in the process
Ok it seems you might have a conception issue...
Let's get this through, if you call me.close(), you are asking your form to terminate itself, and to kill all forms he generated.
However, it is not direct, a windows Message is posted to your application that will be treated whenever you function is done.
Then right after that, you create a new form and you say you want to wait for it to close to continue.
I don't know what your purpose is but you have a few solutions :
If you want to go back to your main form (Form1) when done with LC :
Me.Hide()
LC.ShowDialog()
=>Your code will pick up here when LC will be closed
Me.Show()
If you don't want your Form1 to open ever again, then don't make it your starting form, or start working in the Sub Main()
make the form1 a flowmanager of the forms. I mean, form1 should opens the lc and close the me.
You must call both Dialog Forms from a parent form.
You may do something like this:
PARENT FORM
FirstDialog.Showdialog
(the app will open this dialog and wait it close)
SecondDialog.Showdialog
Doing this way the second dialog will open ONLY when the first one be closed.
You cannot start another dialog from a dialog being closed.
Good luck

Program is running even after the close button pressed

I have a 2 form program,the first form's button click will open the second form when clicked. I tried using a .close() to hide the first form, but that ended the whole program. Then I tried using a .hide(). Now, when I click the X button in the top right of the second form, the program closes, but is still running in the background. How do I get around this?
By default, a VB.NET application will exit when you close the startup form. If you're hiding the startup form and closing the second form then the startup form is still open, so the app won't exit.
If you want to be able to close the first form without exiting the application then go into the project properties and set the shutdown mode to when all forms close instead of when the startup form closes. That way, you can show your second form and then close your first form. Closing the second form will then exit the app.
Simply close the main Form from inside the second form's FormClosed event.
Sub Form2_FormClosed(sender As Object, e As FormClosedEventArgs)
Form1.Close()
End Sub
You should use Me.Close() just before the ShowDialog() function for the second window.
if you want the 1st form to be visible again, add a form1.show() to the form_closing event of the 2nd form
if you want the program to end, add an Application.Exit to the form_closing event of the 2nd form

vb.net - showdialog() sends main window to background

I have a modal window popup so a user of my application can edit some stuff, then they save it and close the window. When they close the popup window, my parent (main) window gets sent to the back of all other applications on my desktop, and then it immediately gets sent back to the front.
Any idea why this would happen?
In your main form:
Dim frmDlg as New FormDialogToShow
frmDlg.ShowDialog(Me)
The main form should not get sent to the back. The child dialog will display on top of the parent. Without the owner reference, the mainform can sometimes get sent to the back. When you dont specify an owner form and that happens:
Dim frmDlg as New FormDialogToShow
frmDlg.ShowDialog()
Me.BringToFront
(the answer is the same as the first time)
Does your modal form somehow hide itself before ShowDialog line ends? This happened to me and was able to solve it by removing the Hide call from the modal form.
I think I read somewhere here in SO that this happens because Windows does not have an enabled window to send focus to in the active app so it sends focus to the next app instead.
This code seems to solve the problem:
' When closing the subform
' ------------------------
sub_form.close()
main.focus()
sub_form.dispose()
When doing this, my main form does not get sent to the back even when the sub form is modal window.
I was searching desperately to find the answer to a similar problem. I found this to be particularly useful:
Private Sub Frm_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
Prompting = False
Frm = Nothing
FrmPrompt.Close()
FrmPrompt.Dispose()
FMain.Activate()
End Sub
Activate allowed my main form to not be sent behind anything else I had open.

Closing forms in VB.NET

I have a small requirement and that is as follows:
I have a login form and after a successful login, i open another MDI form, but i want to close the login form. I have given the code as:
MDIForm1.show
Me.Close
This does not seem to work.
Can anyone help on this.
Similarly, is is possible to open the login form on an inactive MDI form [MDI and Login for to be seen] and when th user login details are correct, it must close the login form and return to the MDI Form.
Please help on this.
Regards,
George
Firstly, I'm guessing you've set your login form as the application's startup form? If so then VB has put something like this together for you behind the scenes:
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
Application.Run(New LoginForm)
The magic happens there in that Application.Run call, which basically creates a message loop for your GUI and terminates when it detects that your form has been closed.
There are a couple of ways that I know of to deal with this.
First: don't make the login form your startup form at all. Create your main form, but cause it to hide itself and ShowDialog on an instance of your login form before appearing.
Make your login form your startup object, but instead of closing it when you open your main form, simply hide it (this is actually pretty horrendous to me personally, but it's a possibility).
As for the MDI form question: this seems completely possible to me, if I understand you correctly. You want to show both the login form and the MDI form at once, but you want the MDI form to be disabled until the login form is closed, right?
So just make the MDI form your application's startup form, have it call ShowDialog on your login form in its Load event, and bingo: the MDI form will effectively be "locked" until the login form is dealt with (this is how modal dialogs in general such as MessageBox work). This is actually the same as option #1 above except that the main form (your MDI form) is never hidden, only blocked.
Other ways:
Dim <form name> As New <form name>
<form name>.Show()
Me.Hide()