如果对话框关闭,如何退出子程序? [英] How to exit a sub if a dialog is closed?
问题描述
我有一个子控件,当用户单击按钮时,该子控件打开一个文件对话框,然后使用文件名将其解压缩(我的程序解压缩zip文件).如果用户未选择.zip文件,则会弹出一条消息,提示您选择正确的格式.这工作正常,除非他们取消了打开的文件对话框,消息仍会弹出,所以如果用户取消,有没有办法退出子程序?这是一些代码:
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.
推荐答案
您只需要检查从模态形式返回的DialogResult
.这样,您的代码仅在他们从表单中获取ok
时才执行.
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
这篇关于如果对话框关闭,如何退出子程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!