Closing a Dialog Form and Open another form - vb.net

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

Related

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.

How to properly close form1 after showing form2

I have a script editor that I'm using to code through the object model of software my company uses. It has a login form to make sure that you are able to access the system and a main form to pull the script from the object and edit using ScintillaNET.
The issue that I am having is when I pass the object to the main form and leave the login form open it runs smoothly but I want to close the login form after the main form is open. When I do this it stops immediately after opening the main form.
Here is a sample of my code. You can see for now that I have commented out the line Me.Close() as that seems to the issue.
' Close this form, show the main form and pass the M3 object to it
Dim f As FormMain = New FormMain
' Pass the M3 object to next form
f.M3System = M3System
f.Show()
'Me.Close()
any help would be awesome!!!
This is a built-in feature for VB.NET. Use Project + Properties, Application tab.
Change the Shutdown mode setting to "When last form closes".

vb.net Prevent Form 1 from activating when form 2 is clicked

im having a similar problem like the solution here Prevent main form from appearing when showing another form . but some of the suggestions were to minimize the main app so it doesnt show, which i cant do because my main app is supposed to be a desktop to be underneath all other apps to replace the windows desktop. And the second forms are supposed to be sticky notes. so i cant minimize the main window cause it has the user background and other controls. i tried making the parent of the notes a Nothing pointer, a pointer to the desktop, creating the form through a dll but i had no success.
My main problem is that when i click a note (form2) form1 comes up, even with form1 having the WS_EX_NOACTIVATE in the createparams. form1 does the form2.show() but they shouldn't be attached.
Another reason im having trouble with the solutions preseted in that post is that they are for delphi and im doing it in vb.net.
All i need is being able to click on the controls and write in the note without bringing the main form behind the note. either making them independent, or making the note not focusing the first form or being able to operate the note without it activating. i dont know. my last resource is to attach my main form to the desktop but i've heard is the worst thing you can do because it can cause problems hanging the system.
If you want both forms to co-exist but don't each to interfere with the other: In this case, then you might want to have a third Form that calls both Form1 and Form2 to be opened, and let me suggest and MDI Form with Form1 and Form2 as children forms of the MDI form
'============== my previous post ========================
You can force the user to first dismiss Form2 then allow him to go back to form1 by showing Form2 as Modal form. Here is how to display Form2 as modal
Dim f2 as New Form2
f2.ShowModal()
If that does not work, try this
Dim f2 as New Form2
f2.Show(True)

VB.NET: How to close and re-open a dialog in this case?

I'm developing a WinForms app in VB.NET, that handles sets of style data, and when the user clicks on another set's label, it prompts through a dialog "You are leaving this style preset to edit another one. keep changes on this one? [Yes] [No]"
But, I'm facing the problem that, when the user clicks either option, and the dialog closes, everything has to be refreshed, and loading the form again seems a good option.
I've tried putting a public sub on a module, that does this:
Public Sub CloseOpenStyleDlg()
KeepOrDiscardPrompt.Close()
StylesDlg.Close()
StylesDlg.ShowDialog()
End Sub
But as soon as that sub is called from the prompt, it crashes the application. (doesn't show an error in debug, simply crashes) How should I, from a given dialog, close the dialog, it's parent, and re-open it's parent? (which triggers all the Dialog_Load() code of the parent)
Thanks in advance! :)
You need to instantiate the dialog again. If I take your code for example:
Public Sub CloseOpenStyleDlg()
KeepOrDiscardPrompt.Close()
StylesDlg.Close()
StylesDlg = new StylesDlg()
StylesDlg.ShowDialog()
End Sub
When a form is closed, all resources created within the object are closed and the form is disposed.
If you want to reuse the Window instance use StylesDialog.Hide() function instead.