none
Return Statement Not Functioning as Expected RRS feed

  • Question

  • Hello all,

    I am trying to make the code below behave correctly.  The way it is written, when I hit Cancel in the file dialogue, it doesn't return back to the beginning of the form, it acts as though I clicked the button again, and opens another file dialogue.

    Any ideas would be helpful, thanks!!

    Public Class SignReader
    
        Private Sub Form1_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
            Dim files() As String = e.Data.GetData(DataFormats.FileDrop)
            For Each path In files
                MsgBox(path)
                Dim frm As New Ripper(path)
            Next
        End Sub
    
        Private Sub Form1_DragEnter(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
            If e.Data.GetDataPresent(DataFormats.FileDrop) Then
                e.Effect = DragDropEffects.Copy
            End If
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFile.Click
            Dim fName As String
    
            Dim openFileDialog1 As New OpenFileDialog With {
                .Filter = "All Files|*.*|.sgn|*.sgn|.txt|*.txt",
                .Title = "Select an SGN, DB, or Downback File"
            }
    
            If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
                fName = openFileDialog1.FileName
                Dim frm As New Ripper(fName)
                'MsgBox(fName)
            ElseIf openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.Cancel Then
                Return
            Else
                MsgBox("Please select a valid file.", MsgBoxStyle.Exclamation, "Invalid File Type")
            End If
        End Sub
    
    End Class

    Thursday, December 27, 2018 8:56 PM

Answers

  • stopiam,

    The problem is that the first If of your code looks at the return value and if it is cancel then executes the next elseif and shows the dialog again.

    You only need to show the dialog once and then check the return value many times.

    Its hard to know exactly what you want to do. The example below uses a  loop so the filename can be checked and if invalid return to the dialog again.

    I tried to follow your example more or less so you could see the logic structure. The select case shows the decisions that are made in a logical way. However I think you want to check for valid name for the OK option as well? So you may want to change that? etc.

    Plus, what do you do with fName? That will need to be a class level (global) variable or something so you can use it in the rest of the application??

    As always there are many ways it could be done.  etc.

    So you will still need to fix and optimize this example  (we purposely leave it obtuse for students). I just tried to show how to replace the if then else you have with this.

    Dim fName As String Dim reOpenDialog As Boolean Dim openFileDialog1 As New OpenFileDialog With { .Filter = "All Files|*.*|.sgn|*.sgn|.txt|*.txt", .Title = "Select an SGN, DB, or Downback File" } Do

                reOpenDialog = False

    Select Case openFileDialog1.ShowDialog() Case System.Windows.Forms.DialogResult.OK fName = openFileDialog1.FileName Dim frm As New Ripper(fName) Case System.Windows.Forms.DialogResult.Cancel Exit Sub Case Else If fName <> "some valid file name critera" Then MsgBox("Please select a valid file.", MsgBoxStyle.Exclamation, "Invalid File Type")

                            reOpenDialog = true

    Else MsgBox("In valid file name.", MsgBoxStyle.Exclamation, "Invalid File Type") End If End Select

    Loop While reOpenDialog





    • Edited by tommytwotrain Friday, December 28, 2018 9:54 AM
    • Proposed as answer by dbasnett Friday, December 28, 2018 2:03 PM
    • Marked as answer by stopiamwarren Friday, December 28, 2018 5:45 PM
    Friday, December 28, 2018 8:07 AM

All replies

  • Hi

    If I understand correctly, it would simply be to remove:

          ElseIf openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.Cancel Then
                Return
            Else
                MsgBox("Please select a valid file.", MsgBoxStyle.Exclamation, "Invalid File Type")
     

    from the code. Unless, maybe you mean the user HAS to select a file, in which case, replacing those lines with:

    Else

    Button1.PerformClick

    might do it.


    Regards Les, Livingston, Scotland

    Thursday, December 27, 2018 9:17 PM
  • stopiam,

    The problem is that the first If of your code looks at the return value and if it is cancel then executes the next elseif and shows the dialog again.

    You only need to show the dialog once and then check the return value many times.

    Its hard to know exactly what you want to do. The example below uses a  loop so the filename can be checked and if invalid return to the dialog again.

    I tried to follow your example more or less so you could see the logic structure. The select case shows the decisions that are made in a logical way. However I think you want to check for valid name for the OK option as well? So you may want to change that? etc.

    Plus, what do you do with fName? That will need to be a class level (global) variable or something so you can use it in the rest of the application??

    As always there are many ways it could be done.  etc.

    So you will still need to fix and optimize this example  (we purposely leave it obtuse for students). I just tried to show how to replace the if then else you have with this.

    Dim fName As String Dim reOpenDialog As Boolean Dim openFileDialog1 As New OpenFileDialog With { .Filter = "All Files|*.*|.sgn|*.sgn|.txt|*.txt", .Title = "Select an SGN, DB, or Downback File" } Do

                reOpenDialog = False

    Select Case openFileDialog1.ShowDialog() Case System.Windows.Forms.DialogResult.OK fName = openFileDialog1.FileName Dim frm As New Ripper(fName) Case System.Windows.Forms.DialogResult.Cancel Exit Sub Case Else If fName <> "some valid file name critera" Then MsgBox("Please select a valid file.", MsgBoxStyle.Exclamation, "Invalid File Type")

                            reOpenDialog = true

    Else MsgBox("In valid file name.", MsgBoxStyle.Exclamation, "Invalid File Type") End If End Select

    Loop While reOpenDialog





    • Edited by tommytwotrain Friday, December 28, 2018 9:54 AM
    • Proposed as answer by dbasnett Friday, December 28, 2018 2:03 PM
    • Marked as answer by stopiamwarren Friday, December 28, 2018 5:45 PM
    Friday, December 28, 2018 8:07 AM
  • This worked but I lost my else statement which I was using to catch invalid file types.  I eventually did this:

            Dim r = openFileDialog1.ShowDialog()
            If r = System.Windows.Forms.DialogResult.OK Then
                fName = openFileDialog1.FileName
                Dim frm As New Ripper(fName)
                'MsgBox(fName)
            ElseIf r = System.Windows.Forms.DialogResult.Cancel Then
                Return
            Else
                MsgBox("Please select a valid file.", MsgBoxStyle.Exclamation, "Invalid File Type")
            End If

    Friday, December 28, 2018 5:41 PM
  • This worked, and actually fixed a different issue I was having with specific file criteria, where the file type could cause the computer to freeze.
    Friday, December 28, 2018 5:45 PM