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

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.

Related

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

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

Form maximize on another form

I have an MDI application which contains many child forms
My problem is, on clicking a particular menu, I am opening a form with maximize window in the MDI form. This works fine.
Now if I open another form above the first one, and if i want the second form to be of normal size, i am unable to do it.
Second form also opens with maximized window similar to first one. I want the second form to be of normal small size.
I want to show second form normally and first form maximized.
How can i do that?
Private Sub TESTToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles TESTToolStripMenuItem.Click
Dim f As New newCalendar2("UGHARANI")
f.Show()
f.MdiParent = Me
f.WindowState = FormWindowState.Maximized
End Sub
Okay, I think I understand what you're trying to accomplish: you want the first (data) form to be a kind of background to your MDI application and have the other forms display on top of it, right?
Well one way to do it might be to remove borders from the background form –FormBorderStyle = None– and fill-dock it in the MDI parent form. Although it would end up coming to the fore and hiding all your other forms if a user clicked anywhere on it. But if it doesn't require any user interaction you could always use its Activate event to send it back to the background, using Me.SendToBack().

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.