Christian Quirante Christian Quirante - 5 months ago 16
Vb.net Question

OpenFileDialog, I want to use If-Else Statement than Using Try-Catch - VB.NET

How can I do an If-Else Statement on a OpenFileDialog.ShowDialog() like this?

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

If MsgRegisterComplete = vbYes Then

ElseIf MsgRegisterComplete = vbNo Then

End If


Because I'm currently using try-catch which has flaws on it, like when I selected a picture already to be opened in a single instance of debugging. It wouldn't do any Catch Statements anymore, Unlike the If-Else Statement shown above, I do think It would work much better with OpenFileDialog.

Here is my Current Code on 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


How can I convert this try-catch statements to if-else statements like the code given above?

Answer

Use the return value of OpenFileDialog.ShowDialog

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
Comments