OpenFileDialog,我想使用If-Else语句而不是使用Try-Catch - VB.NET

时间:2016-06-15 02:01:25

标签: vb.net

如何在这样的OpenFileDialog.ShowDialog()上执行If-Else语句?

Dim MsgRegisterComplete As Integer
        MsgRegisterComplete = MsgBox("Sample", MsgBoxStyle.YesNo, "Message Box")

        If MsgRegisterComplete = vbYes Then

        ElseIf MsgRegisterComplete = vbNo Then

        End If

因为我目前正在使用具有缺陷的try-catch,就像我选择在单个调试实例中打开的图片一样。它不会再做任何Catch语句了,与上面显示的If-Else语句不同,我认为OpenFileDialog会更好。

这是我在OpenFileDialog上的当前代码

 Dim strFileName As String

    OFDViewEditUpdateForm.InitialDirectory = "d:\"
    OFDViewEditUpdateForm.Title = "Select Picture"
    OFDViewEditUpdateForm.Filter = "JPEG images|*.jpg;*.bmp;*.png"
    OFDViewEditUpdateForm.ShowDialog()

    Try
        strFileName = OFDViewEditUpdateForm.FileName
        PBUserImage.Image = Image.FromFile(strFileName)
        LBDUserImageLink.Text = strFileName
    Catch
        MsgBox("No selected image has been made")
    End Try

如何将此try-catch语句转换为if-else语句,如上面给出的代码?

1 个答案:

答案 0 :(得分:1)

使用Railscast 287

的返回值
Dim strFileName As String

OFDViewEditUpdateForm.InitialDirectory = "d:\"
OFDViewEditUpdateForm.Title = "Select Picture"
OFDViewEditUpdateForm.Filter = "JPEG images|*.jpg;*.bmp;*.png"
if (OFDViewEditUpdateForm.ShowDialog() = Windows.Forms.DialogResult.OK) Then
  strFileName = OFDViewEditUpdateForm.FileName
  ' Do whatever else you want to do
Else
  MsgBox("No selected image has been made")
End If