Question

I have a function which checks for the existance of a file (return file.exists(file)). If it does not exist, then I display an error message with options Abort, Retry, Ignore.

My trouble is I can't get it to retry.

I have tried putting the code to check if the file exists in a seperate function, then calling that function from the retry case of the select case statement, but it seems to go right past it (because it already knows it doesn't exist?) I tried creating a separate class containing the function to check if the file exists, then creating a new instance of that class every time I call it but that didn't help.

Am I missing something?

I want the application to keep checking again every time the user clicks retry, until they press either abort or ignore (or of course it does find the file.

What is the proper way to handle the retry?

Private Sub main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    If CheckFileExists() Then
     'do stuff here
    End If
End Sub

Private Function CheckFileExists()
    If Not FindFile() Then
        Select Case MessageBox.Show("Can't Find File", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error)
            Case Windows.Forms.DialogResult.Abort
                End
            Case Windows.Forms.DialogResult.Retry
                Return FindFile()
            Case Windows.Forms.DialogResult.Ignore
                MessageBox.Show("Proceeding without file present")
                'do some other stuff
                Return True
            Case Else
                Return False
        End Select
    Else
        Return True
    End If
End Function

Private Function FindFile() As Boolean
    Return System.IO.File.Exist(path\file.ext)
End Function

I've also tried putting it into a class:

Private Function FindFile() As Boolean
    Dim fc As New FileCheck
    If Not fc.fnFileCheck() Then
        Return False
    Else
        Return True
    End If
End Function


Public Class FileCheck
Public Function fnFileCheck() As Boolean
    Return System.IO.File.Exist(path\file.ext)
End Function
End Class
Était-ce utile?

La solution

If you want to keep on checking the file until either abort or ignore are pressed I think you have to call CheckFileExists() instead of FindFile() in the "retry" case

Private Function CheckFileExists()
    If Not FindFile() Then
        Select Case MessageBox.Show("Can't Find File", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error)
            Case Windows.Forms.DialogResult.Abort
                End
            Case Windows.Forms.DialogResult.Retry
                Return CheckFileExists()
            Case Windows.Forms.DialogResult.Ignore
                MessageBox.Show("Proceeding without file present")
                'do some other stuff
                Return True
            Case Else
                Return False
        End Select
    Else
        Return True
    End If
End Function
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top