I have a sub that opens a file dialog when the user clicks a button, then uses the file name to extract it (my program extracts zip files). If the user does not pick a .zip file a message pops up telling to select the correct format. This worked fine except if they canceled the open file dialog the message would still pop up so is there a way to exit the sub if the user cancels? Here is some code:
Private Sub AddJarMod_Click(sender As Object, e As EventArgs) Handles AddJarMod.Click
addModDialog.ShowDialog()
newJarModDir = addModDialog.FileName
newJarMod = System.IO.Path.GetFileNameWithoutExtension(newJarModDir)
If System.IO.Path.GetExtension(newJarModDir) = ".zip" Then
jarModList.Items.Add(newJarMod)
devConsoleList.Items.Add(newJarModDir)
ElseIf System.IO.Path.GetExtension(newJarModDir) <> ".zip" Then
MsgBox("File extension not a zip")
End If
End Sub
I'm relatively new to coding and the forums so sorry if my code or the post isn't completely correct.
You just need to check the DialogResult
being returned from the modal form. This way your code only executes if they get an ok
from the form.
Private Sub AddJarMod_Click(sender As Object, e As EventArgs) Handles AddJarMod.Click
If addModDialog.ShowDialog() = DialogResult.Ok Then
newJarModDir = addModDialog.FileName
newJarMod = System.IO.Path.GetFileNameWithoutExtension(newJarModDir)
If System.IO.Path.GetExtension(newJarModDir) = ".zip" Then
jarModList.Items.Add(newJarMod)
devConsoleList.Items.Add(newJarModDir)
ElseIf System.IO.Path.GetExtension(newJarModDir) <> ".zip" Then
MsgBox("File extension not a zip")
End If
End If
End Sub