close form when application loses focus - vb.net

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

Related

Prevent old form closing until new form opens

A very basic/simple question with I'm sure, an even simpler answer, but I just cannot figure it out. I have two forms that a user can switch between using the corresponding menu link on each form. I want to be able to keep the previous form visible on screen until the new form is displayed. In it's current state, the form disappears off screen for around 3/4 of a second before the new one is shown and from a UI/design perspective, I'd like this to stay on screen.
I'm currently using the below code to close and open the forms:
form1.Show()
Me.Close()
form2.Show()
Me.Close
I have tried experimenting with ShowDialog() which does seem to keep it on screen on first run, but clicking back into the form a second time says an error message:
System.InvalidOperationException: 'Form that is already visible cannot be displayed as a modal dialog box. Set the form's visible property to false before calling showDialog.'
Is there a simple line of code to achieve what I want here?
If there is some time consuming code in the Form.Load event (for example, if data is being retrieved from a database) then the following code might help.
Private Sub Form2_Shown(sender As Object, e As EventArgs) Handles Me.Shown
'Assuming default instances
Form1.Close()
End Sub
Posting an answer incase there's an inherit reason this is not best practice or to avoid doing this, but using the below sped it up:
Private Sub menu1_Click(sender As Object, e As EventArgs) Handles menu1.Click
form1.Show()
form1.Refresh()
Me.Close()
End Sub
This was inspired by adding in a Application.DoEvents working, so adjusted the code to avoid that dreaded line.

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.

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.

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 activated fires multiple times

I want to update a database based on the form that is currently activated. I had originally decided to use the GotFocus event. However I now understand that will not work as the form has controls on it. So I then thought I wouls use the activated event. This works but seems to fire multiple times. I put in the following code:
Private Sub frmNewTicket_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
MsgBox("Form Activated")
End Sub
I select the form and make it activated and the message box appears about 15 times.
Why does it do this? How should I handle this. I only want my code to execute once when the form is activated.
NOTE: There are several forms that the users will be changing between, incuding forms from other applications.
Each time you click OK on the messagebox, the form regains the focus and is activated again.
Put a static Boolean value in your frmNewTicket_Activated like someone has posted here:
Static HasRan As Boolean=False
If Not HasRan Then
HasRan=True
'put code here
End If
It sounds like you are wanting to do something everytime your form gets activated. The Form Activated event will work fine as long as what you are doing doesn't pull focus from the Form which will then trigger another Activation event when the Form gets focus again. Try using something other than a MessageBox for testing like Beep or changing the Form's Color