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
Related
I've created a Form in excel 2017 with a "Cancel" button, but I don't know how to code it so that it closes itself when clicked. I've found things like "Application.Quit" but it closes everything.
There are two ways to achieve that. Both will only close your form and keep Excel opened.
I've asked a question about it a while ago maybe this can help you.
1. Hide Method
Your userForm has a Hide method, you can call it to hide your form.
Example:
Private Sub btnCancel_Click()
yourFormName.Hide
End Sub
Using the Hide method will only hide the form, it will be totally closed when you close your Excel file.
If you make changes in the form, hide it and then show it again all changes will be kept (ex: change the value of a textbox before hiding it and it will stay the next time you show your form).
The Activate event won't be triggered next time you show the form since the form was still active but hidden.
2. Unload Method
You can call the Unload method to unload your form.
Example:
Private Sub btnCancel_Click()
Unload me
End Sub
The form is unloaded from the memory. If you make changes in your form and then unload it, it won't be kept next time you show your form unlike with the Hide method.
You can use Unload Me to close the Userform.
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
I'm using vb.net 2013, and I have configured Shutdown mode to "When last form is closed".
On my main form, I have a menu item which has this code to close the application:
Application.Exit
Everything is working fine, except one case:
When I open a specific form, where a Combobox is created on runtime and I've used Addhandler to subscribe to several events.
The combobox is created when pressing a button.
When I open this form and I don't create the combobox, everything is working ok. If the combobox is created, when I close this form and try to close the application using the menu item, nothing happens. The application is not closed and no error message is displayed. (the same situation occurs when I try to close the main form with "x" button)
On the form's (where I have the combobox) close event , I tried to put a line of code:
MyCombobox.dispose()
But the situation is the same.
What can I do? Thank you!
What I do from my little experience is
1.) remove the MyCombobox from its parent control (I'm think this is in your combobox close event).
2.) set the MyCombobox to Nothing
3.) Dispose() it.
MyCombobox = Nothing
MyCombobox.Dispose()
It would be useful to see some part of your code for the close event so we can help you check. More power to you!
Update based on OP's comment:
I have read the following from MSDN: https://msdn.microsoft.com/en-us/library/system.windows.forms.form.closing(v=vs.110).aspx
From this, it is important to note that:
The Form.Closed and Form.Closing events are not raised when the Application.Exit method is called to exit your application. If you have validation code in either of these events that must be executed, you should call the Form.Close method for each open form individually before calling the Exit method.
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.
I'm using VB.NET (VS2008) and have a form (frmMain) that calls another form (frmCleanUp) but when frmCleanUp shows it can be hidden behind frmMain.
What I'd like to have is the new form be locked and have to close to get rid of it?
Thanks..
Show the Form with .ShowDialog() instead of .Show().
This way, the form is shown as modal dialog box and will stay on top until you close it.
when you show the form, do it with ShowDialog
in frmMain do:
frmCleanUp.ShowDialog