Question Catch Select

solfinker

Well-known member
Joined
Aug 6, 2013
Messages
71
Programming Experience
Beginner
Hello.
I am having problems with a Case statement, and my application does not stop with the following code:
VB.NET:
 Try
...
 Catch ex As Exception
            Select Case MsgBox("message", MsgBoxStyle.YesNoCancel, "caption")
                Case MsgBoxResult.Yes
                    MessageBox.Show("message yes", "caption") : Application.Exit()
                Case MsgBoxResult.Cancel : Application.Exit()
                Case MsgBoxResult.No : Exit Select
            End Select
End Try

Is it that I only can have a single statement in every Case ?
 
You can have as many lines of code as you want in a Case block. Now get rid of those colons and never use them again. Also, get rid of that Exit Select. If you don't want to do anything on No then either leave that Case empty or don't even create a Case for it.
 
The colons were my second option, as the first did not work either. Or at least I could not exit the application on Application.Exit()

Thank you.

I have a similar case herein:

VB.NET:
If Booleanfield then 
  MsgBox("Quit")
  Application.Exit()
End if

but the application does not exit
 
Last edited:
So the real issue is that you call Application.Exit and the application does not exit. That's what you should have said in the first place.

The first question is whether you are handling a FormClosed event anywhere and cancelling it. If so then that is most likely the reason. If not then something may be broken, so the first thing I'd check is whether Application.Exit works as expected in a new project.
 
Application.Exit works in the same application, in the same form, out of a button_click event, out of a Loop, this way:

VB.NET:
Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
        [COLOR=#40e0d0]Application[/COLOR].Exit()
End Sub


Private Sub Timer1_Tick(ByVal Sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
[COLOR=#0000ff]Do While[/COLOR]
Try
Catch
End  Try
[COLOR=#0000ff]If[/COLOR] booleanfield [COLOR=#0000ff]Then[/COLOR]
  Msgbox("Exit")
  [COLOR=#40E0D0]Application[/COLOR].Exit()
[COLOR=#0000ff]End if[/COLOR]
Try
Catch
End Try
[COLOR=#0000ff]Loop[/COLOR]
End Sub

Thank you very much for your help.
 
Back
Top