E.cancel loop in Visual basic - vb.net

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.

Related

Auto close application

I'm new to this site and VB, so please keep that in mind.
I have a program which basically connects to our database and updates it, but what I want is after this code:
Private Sub Button1_Click(ByVal sender As system.EventArgs) Handels Button1.Click
If Retry = 0 then
End
Else
TextBox3.Textt = ""
Button1.text = "Ok"
Button1.enabled = False
Me.refresh()
System.Threading.Thread.Sleep(500)
Me.Form1_Load(Me, e)
System.Threading.Thread.Sleep(500)
Me.Refresh()
System.Windows.Form.Application.DoEvent()
End If
End Sub
End Clas
This form/App to close (can be closed before this part as it just tells me everything went well but still if you click ok will retry) so I don't have to close it every time I run this app to update and then annoyingly stay awake as it won't let me study the code nor let me change anything until this app is closed. sometime it takes so long that i even forget i have this app running.
I can provide more info if tell me what otherwise I don't know much about VB.
You can use Me.Close() it will close the form when executed.
Or Application.Exit() you can check HERE

How do you bypass a FormClosing event in vb.net?

In my mdiMain, I have this code set.
Private Sub mdiMain_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
e.Cancel = IIf(MsgBox("Closing this window will log you out. Are you sure?", MsgBoxStyle.YesNo, "Log out?") = MsgBoxResult.Yes, False, True)
End Sub
My reason for this is to catch all triggers of closing the form. Either if the user clicks the log out button, or they press the x button on the top right corner of the window, or even if they press Alt-F4. Under the FormClosed of the same MDI, there is a set of instructions after it has been confirmed to log out. My problem is I also have a process where in the user is forcibly logged out of the program. Since the user is "forced" to log out, the confirmation dialog should be bypassed. But I'm not sure how to bypass the FormClosing event, and skip directly to the FormClosed.
So far I've only come up with one way and that is to set a boolean trigger. Something like
Private Sub mdiMain_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
If ForceLogOut = True Then Exit Sub
e.Cancel = IIf(MsgBox("Closing this window will log you out. Are you sure?", MsgBoxStyle.YesNo, "Log out?") = MsgBoxResult.Yes, False, True)
End Sub
But for knowledge's sake, I would still like to hear from other people about other ways for this.
You can check the FormClosingEventArgs.CloseReason to see why the form is closing i.e. what event caused the form to close and set the cancel flag accordingly.
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Select Case e.CloseReason
Case CloseReason.TaskManagerClosing
e.Cancel = False
Case CloseReason.MdiFormClosing
e.Cancel = If(MsgBox("Closing this window will log you out. Are you sure?", MsgBoxStyle.YesNo, "Log out?") = MsgBoxResult.Yes, False, True)
'etc
End Select
End Sub
Note: You should use the IfOperator not the IIf function in VB.NET as it is type safe. You should also switch option strict on (which incidentally will not allow you to use IIf for this very reason)

Form_Closing event in vb.net launched twice

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

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.

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