我的自定义 ScrapeFileOptionDialog 窗口显示两次

问题描述 投票:0回答:1

当我关闭第一个弹出窗口、中途关闭它或将结果保存到 Excel 后,我的自定义对话框显示两次。我有一种感觉,我不小心把它称为错误的。任何指出我哪里出错以及为什么会发生这种情况的指示都将极大地帮助我的研究。

这是 ScrapeFileButton_Click 代码:

    ' 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

我认为这个方法/处理程序出了问题。我查看了其他方法和初始化程序,似乎看不到其他方法和初始化程序的问题。预先感谢。

vb.net
1个回答
0
投票

解决方法是我假设表单顶部的关闭 (X) 与对话框上的“取消”按钮具有相同的处理程序。一旦我添加了关闭函数的逻辑来操作它就停止重复 ShowDialog 选项。

© www.soinside.com 2019 - 2024. All rights reserved.