Form_Closing event in vb.net launched twice - vb.net

I'm developing a simple app in vb.net (vs2010). I'm using the Form_Closing event in the following way:
Private Sub frmPrincipal_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If MessageBox.Show("Quit", "Are you sure?", _
MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
Me.Close()
Else
e.Cancel = True
End If
End Sub
This code shows a dialog asking the user if he really wants to leave the app.
I the answer is 'No', it works fine, but if the answer is 'Yes', it seems that the event Form_Closing is launched again, and the same dialog is showed again, asking if the user really wants to exit the app... The answer in this second dialog is not evaluated by the app, this means, the app will end after this second dialog.
My question is, how to avoid this second dialog when the user select 'Yes' on it?
Regards,
Daniel.

You shouldn't need to call Close() a second time.

You have already Closed the Form and again Calling the same pressing Yes so in place of
Me.Close
Use
End

Related

Avoid refresh event in a WebBrowser control?

I am using the GEPluginControl in VB.NET (VS2010).
My app works fine. I am using the GEWebBrowser control (included on the GEPluginControl), and I am able to show the information in the Google Earth format.
However, I am having troubles when the GEWebBrowser control Refresh method is called. When this happens, the GEWebBrowser starts to fail (the earth disappears and I am not able to reload it again).
I am not calling the Refresh method explicitly. I think this method is called automatically in the following case: when the user close the window, I'm catching the event Form_Closing, in order to ask the user if really wants to exit. If the answer is No, the user will stay on the app, but the GEWebBrowser control appears blank!. I can hear the sound associated to the refresh method, so I think this method may be my trouble.
Here is the Form_Closing event code:
Private Sub frmPrincipal_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If MessageBox.Show("Quit", "Really want to exit?", _
MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
'App end.
server.Stop()
Else
'User decides to stay.
e.Cancel = True
End If
End Sub
Does anybody knows how to avoid the WebBrowser (GEWebBrowser in this case) to execute the refresh method? If anyone knows how to solve this in any other way, I would still be very grateful.
You could try this (not guaranteed to work):
Private Sub StopRefresh(sender As System.Object, e As System.EventArgs) Handles WebBrowser1.ProgressChanged
WebBrowser1.Stop()
End Sub

Microsoft Office BeforeCloseHandler

I made a function in vb.net for desktop application that totally saved the documents before closed, I implemented it with the System.Threading.Timer.
I'm asking with favor on how you experienced it.
So, I made a timer to saved the documents before closed.
But, upon testing without the timer, just one call to the function or Handler, the documents are not saved before closed, because a dialog box appears asking to saved or not or cancel.
Is it really in need to have the timer? because we know a timer could delay the system even a single milliseconds.
I setup the timer with 100 milliseconds.
But, I don't want to use the timer, I want to saved the documents before closed for just one call of the function.
Is it really using the timer is the solution? or it can be done with just one call setup?
If it can be done in one function call, then I think I'm missing a code.
Thanks for your opinion and experienced.
You can handle the FormClosing event. In the handler you can perform actions before the form closes you can even cancel the closing event. Here's a an example:
Dim Saved As Boolean = False
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If Not Saved Then
Dim Result As DialogResult = MessageBox.Show("Do you want to save your text?", "Save Text?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
Select Case Result
'Since pressing the No button will result in closing the form without
'saving the text we don't need to handle it here
Case Windows.Forms.DialogResult.Yes
SaveText_Click()
Case Windows.Forms.DialogResult.Cancel
e.Cancel = True
End Select
End If
End Sub
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
Saved = False
End Sub
'SaveText is a button for the user to manually save the text on demand.
'Since we don't need the sender or the eventargs, we can handle the click event without them.
'this way we can call this like any sub without having to worry about providing the right parameters.
Private Sub SaveText_Click() Handles SaveText.Click
'Add your procedure to save the text here
Saved = True
End Sub
If you don't want the option to close without saving just omit the messagebox and the select block and just call SaveText() and include the code to save your data in there.

E.cancel loop in Visual basic

I am making an server control application ( simple with some buttons to start/stop the server )
And when the user wants to close the application there will be prompted an confirm box.
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Dim response As Integer
response = MsgBox("Are you sure you want to stop the server", vbYesNo, "Stop Server ?")
If response = vbYes Then
Shell("cscript ""stop.vbs""", 1)
Close()
Else
e.Cancel = True
End If
End Sub
That is the code I have now.
But when I start the application and close it with the X button or with "Close Window" I will be prompted with the question until I click on no, then it will close.
It's a loop and it stops when you first click on yes then on no.
Can someone help me with solving this ?
Just remove the Close() call, since the Form is already closing. No need to do that.

Me.Close on Windows 7/2008

I'm in the process of writing a VB app that launches at user login. The issue I see is that on Windows 7/2008 when closing the last form using both Me.Close or Application.Exit both throw the error message "did the application close properly". On WindowsXP it closes without issue.
One of the basic features of the app is that when loading it checks some values and closes the app based on those values. Even in this instance I get the "did not close properly" message on win 7/2008. What could I be doing wrong?
Private Sub Dialog1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If fName = failsafeUser Then
allowLogin = vbTrue
Process.Start(My.Settings.ExplorerPath)
'Me.Close() ' TODO Check into why Me.Close() is ending poorly on 2008 servers
Application.Exit()
End If
End Sub
try using
End
to close your app. That should work. Obviously application.Exit is the better option, but End should work.
Thanks
Paul

Application.Exit() and FormClosing event in Vb.net

I have a single windows form application that is running in system tray icon.If the user press X button of the windows form a messagebox is displayed with Yes and No ( Yes ->close the form---No->keep the form running in system tray icon).
I was thinking to prevent the scenario when the user open another instance of the application when there is already an instance running so i have used this code :
If Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName).Length> 1 Then
MessageBox.Show("Another instance is running", "Error Window", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation)
Application.Exit()
End If
The problem is that when i want to test this the message is displayed but after i press ok, a new messagebox appears (that one from Private Sub Form_FormClosing ).If i choose NO i will have to instance running!
I have read that Application.Exit fires the Form_FormClosing event.
Is there any possibility to cancel the triggering of the Form_FormClosing event,or am i doing something wrong?
'this is the formclosing procedure
Private Sub Form_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Try
Dim response As MsgBoxResult
response = MsgBox("Are you sure you want to exit", CType(MsgBoxStyle.Question + MsgBoxStyle.YesNo, MsgBoxStyle), "Confirm")
'If the user press Yes the application wil close
'because the application remains in taskmanager after closing i decide to kill the current process
If response = MsgBoxResult.Yes Then
Process.GetCurrentProcess().Kill()
ElseIf response = MsgBoxResult.No Then
e.Cancel = True
Me.WindowState = FormWindowState.Minimized
Me.Hide()
NotifyIcon1.Visible = True
End If
PS: I am not a programmer so please don't be to harsh with me:)
You don't need to Kill the current process or use the End Statement. If you have to use these then there is something amiss with your application.
When you want to end your application use Me.Close. This will fire the FormClosing event:
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Select Case MessageBox.Show("Are you sure you want to exit?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
Case Windows.Forms.DialogResult.Yes
'nothing to do here the form is already closing
Case Windows.Forms.DialogResult.No
e.Cancel = True 'cancel the form closing event
'minimize to tray/hide etc here
End Select
End Sub
To stop more than one copy of your application from running use the option to Make Single Instance Application
In the situation where you are just starting your application and are testing for previous instances I have used the VB End Statement to terminate the application.
The End statement stops code execution abruptly, and does not invoke
the Dispose or Finalize method, or any other Visual Basic code. Object
references held by other programs are invalidated. If an End statement
is encountered within a Try or Catch block, control does not pass to
the corresponding Finally block.
If Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName).Length> 1 Then
MessageBox.Show("Another instance is running", "Error Window", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End
End If
Private Sub main_master_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If e.CloseReason = CloseReason.UserClosing Then
'Put you desired Code inside this!
Msgbox("Application Closing from Taskbar")
End If
End Sub
It will Close the exe from Taskbar or kill Process. If user Close the
Application from taskbar.
CloseReason.UserClosing
event will close the application if it is closed by User from
Taskber