My custom ScrapeFileOptionDialog window is showing twice

  Kiến thức lập trình

My custom dialog is showing twice when I either close the first window popup, after I close it midway, or after it saves the results to Excel. I have a feeling that I am calling it accidentally wrong somehow. Any pointers to where I am going wrong and why this is occurring will greatly help me with my research.

Here is the ScrapeFileButton_Click code:

    ' Event handler for the scrape file button click event
Private Sub ScrapeFileButton_Click(sender As Object, e As EventArgs) Handles scrapeFileButton.Click
    Dim optionsDialog As New ScrapeFileOptionsDialog()
    Dim optionsResult As DialogResult = optionsDialog.ShowDialog()

    If optionsResult = DialogResult.OK Then
        Dim searchTerm As String = InputBox("Enter the search term:")

        If String.IsNullOrWhiteSpace(searchTerm) Then
            MessageBox.Show("Please enter a valid search term.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        End If

        If optionsDialog.ScrapeSingleFile Then
            ' User chose to scrape a single file
            Dim openFileDialog As New OpenFileDialog With {
            .Filter = "Supported Files|*.docx;*.htm;*.html;*.pdf",
            .Title = "Select File(s)",
            .Multiselect = False ' Allow selecting only one file
        }

            If openFileDialog.ShowDialog() = DialogResult.OK Then
                Dim filePath As String = openFileDialog.FileName
                Dim fileType = Path.GetExtension(filePath).ToLower()

                Select Case fileType
                    Case ".docx"
                        ProcessWordDocument(filePath, searchTerm)
                    Case ".htm", ".html"
                        ProcessHtmlFile(filePath, searchTerm)
                    Case ".pdf"
                        ProcessPdfFile(filePath, searchTerm)
                    Case Else
                        MessageBox.Show("File type not supported.")
                End Select

                SaveToExcel() ' Save results after processing the selected file
            End If
        ElseIf optionsDialog.ScrapeEntireFolder Then
            ' User chose to scrape an entire folder
            Dim folderBrowserDialog As New FolderBrowserDialog With {
            .Description = "Select Folder",
            .ShowNewFolderButton = False
        }

            If folderBrowserDialog.ShowDialog() = DialogResult.OK Then
                Dim folderPath As String = folderBrowserDialog.SelectedPath
                Dim files As String() = Directory.GetFiles(folderPath)

                For Each filePath In files
                    Dim fileType = Path.GetExtension(filePath).ToLower()

                    Select Case fileType
                        Case ".docx"
                            ProcessWordDocument(filePath, searchTerm)
                        Case ".htm", ".html"
                            ProcessHtmlFile(filePath, searchTerm)
                        Case ".pdf"
                            ProcessPdfFile(filePath, searchTerm)
                        Case Else
                            MessageBox.Show("File type not supported.")
                    End Select
                Next

                SaveToExcel() ' Save results after processing all files in the folder
            End If
        End If
    End If
End Sub

I think it is going wrong in this method/handler. I have looked at other methods and initializers and can’t seem to see a problem with others. Thanks in advance.

LEAVE A COMMENT