If和Else语句导致多个消息框一个接一个地出现

时间:2014-06-27 08:54:22

标签: vb.net

我在这些If和Else声明中处于低迷状态,不是因为他们困惑我而喝茶。我遇到的问题如下:我有5个复选框,1个按钮和一个进度条,手头的问题是当勾选checkbox1时,你单击button1,你会感染多个消息框另一个。你在复选框中勾选的内容并不重要,同样的事情顺序会一遍又一遍地发生,我不能为我的生活弄清楚。我该如何纠正?以下是我需要帮助解决的代码行。

我希望它检查文件目录是否存在,如果它不存在则下载文件。如果它确实存在,那么一旦说出"文件已存在"就会出现一条消息。等等。

Public Sub Server1()
    If (CheckBox1.Checked = True) Then
        If (File.Exists("C:\something1")) Then
            MessageBox.Show("some message", "hi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Else
            S1.DownloadFileAsync(New Uri("https://dl.dropboxusercontent.com/something1"), "C:\something1")
        End If
        If (File.Exists("C:\something2")) Then
            MessageBox.Show("some message", "hi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Else
           S2.DownloadFileAsync(New Uri("https://dl.dropboxusercontent.com/something2"), "C:\something2")
        End If
        If (CheckBox2.Checked = True) Then
        ElseIf (File.Exists("C:\something3")) Then
            MessageBox.Show("some message", "hi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Else
            S3.DownloadFileAsync(New Uri("https://dl.dropboxusercontent.com/something3"), "C:\something3")
        End If
        If (File.Exists("C:\something4")) Then
            MessageBox.Show("some message", "hi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Else
            S4.DownloadFileAsync(New Uri("https://dl.dropboxusercontent.com/something4"), "C:\something4")
        End If
        If (CheckBox3.Checked = True) Then
        ElseIf (File.Exists("C:\something5")) Then
            MessageBox.Show("some message", "hi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Else
            S5.DownloadFileAsync(New Uri("https://dl.dropboxusercontent.com/something5"), "C:\something5")
        End If
        If (File.Exists("C:\something6")) Then
         MessageBox.Show("some message", "hi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Else
            S6.DownloadFileAsync(New Uri("https://dl.dropboxusercontent.com/something6"), "C:\something6")
        End If
        If (CheckBox4.Checked = True) Then
        ElseIf (File.Exists("C:\something7")) Then
         MessageBox.Show("some message", "hi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Else
            S7.DownloadFileAsync(New Uri("https://dl.dropboxusercontent.com/something7"), "C:\something7")
        End If
        If (File.Exists("C:\something8")) Then
       MessageBox.Show("some message", "hi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Else
            S8.DownloadFileAsync(New Uri("https://dl.dropboxusercontent.com/something8"), "C:\something8")
        End If
        If (File.Exists("C:\something9")) Then
         MessageBox.Show("some message", "hi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Else
            S9.DownloadFileAsync(New Uri("https://dl.dropboxusercontent.com/something9"), "C:\something9")
        End If
        If (File.Exists("C:\something10")) Then
         MessageBox.Show("some message", "hi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Else
            S10.DownloadFileAsync(New Uri("https://dl.dropboxusercontent.com/something10"), "C:\something10")
        End If
        If (File.Exists("C:\something11")) Then
        MessageBox.Show("some message", "hi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Else
            S11.DownloadFileAsync(New Uri("https://dl.dropboxusercontent.com/something11"), "C:\something11")
        End If
        If (File.Exists("C:\something12")) Then
         MessageBox.Show("some message", "hi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) 
        Else
            S12.DownloadFileAsync(New Uri("https://dl.dropboxusercontent.com/something12"), "C:\something12")
        End If
        If (CheckBox5.Checked = True) Then
        ElseIf (File.Exists("C:\something13")) Then
         MessageBox.Show("some message", "hi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Else
            S13.DownloadFileAsync(New Uri("https://dl.dropboxusercontent.com/something13"), "C:\something13")
        End If
        If (File.Exists("C:\something14")) Then
         MessageBox.Show("some message", "hi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) 
        Else
            S14.DownloadFileAsync(New Uri("https://dl.dropboxusercontent.com/something14"), "C:\something14")
        End If
        If (File.Exists("C:\something15")) Then
        MessageBox.Show("some message", "hi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) 
        Else
            S15.DownloadFileAsync(New Uri("https://dl.dropboxusercontent.com/something15"), "C:\something15")
        End If
        If (File.Exists("C:\something16")) Then
        MessageBox.Show("some message", "hi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) 
        Else
            S16.DownloadFileAsync(New Uri("https://dl.dropboxusercontent.com/something16"), "C:\something16")
        End If
        If (File.Exists("C:\something17")) Then
         MessageBox.Show("some message", "hi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Else
            S17.DownloadFileAsync(New Uri("https://dl.dropboxusercontent.com/something17"), "C:\something17")
        End If
    End If
End Sub

2 个答案:

答案 0 :(得分:3)

您的问题是您正在独立于最后一个文件检查每个文件,并且每个If块都有它自己的消息部分。

由于您反复重复使用相同的代码,这进一步蒙上了阴影。您应该将重复的代码移动到一个函数中并调用它。此外,您可以将文件添加到某种类型的数组对象中,并使用循环 - 这样您可以在以后添加更多文件而无需添加块 - 您甚至可以将其连接到某种配置文件等,所以你可以完全避免更改代码。

Private Sub GetFile(ByVal SourceUrl As String, ByVal TargetFileDirectory As String)
    Dim FileName As String = SourceUrl.Substring(SourceUrl.LastIndexOf("/") + 1)
    Dim TargetFilePath As String = Path.Combine(TargetFileDirectory, FileName)


    If (File.Exists(TargetFilePath)) Then
        MessageBox.Show("some message", "hi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    Else
        S1.DownloadFileAsync(New Uri(SourceUrl), TargetFilePath)
    End If
End Function

Public Sub Server1()
    Dim items As New Dictionary(Of CheckBox, String)
    items.Add(CheckBox1, "https://dl.dropboxusercontent.com/something1")
    items.Add(CheckBox2, "https://dl.dropboxusercontent.com/something2")
    items.Add(CheckBox3, "https://dl.dropboxusercontent.com/something3")
    items.Add(CheckBox4, "https://dl.dropboxusercontent.com/something4")
    '..... Add more as required.

    Dim TargetDirectory As String = "C:\"

    For Each chk As CheckBox In items.Keys

        If chk.Checked Then
            Dim url As String = items(chk)

            GetFile(url, TargetDirectory)
        End If

    Next

    If (CheckBox1.Checked = True) Then
        For Each item As String In items
            GetFile(item, TargetDirectory)
        Next
    End If
End Sub

我已经采取了重构的一些自由来使它更通用,但从功能上来说,这基本上就是你现在正在做的事情。

现在很清楚,每个文件获取都是完全独立的,因此消息不断传递。

有几种方法可以解决这个问题:

  • 您只能显示第一个失败,并停止下载其他所有文件。但这可能不是理想的
  • 您只需告知用户已存在某些消息
  • 即可
  • 您可以返回已存在的文件列表

我将演示最后一篇:

''' <returns>True if the get was successful</returns>
Private Function GetFile(ByVal SourceUrl As String, ByVal TargetFileDirectory As String) As Boolean
    Dim FileName As String = SourceUrl.Substring(SourceUrl.LastIndexOf("/") + 1)
    Dim TargetFilePath As String = Path.Combine(TargetFileDirectory, FileName)

    Dim retVal As Boolean = False

    If File.Exists(TargetFilePath) = False Then
        S1.DownloadFileAsync(New Uri(SourceUrl), TargetFilePath)
        retVal = True
    End If

    Return retVal
End Function

Public Sub Server1()
    Dim items As New Dictionary(Of CheckBox, String)
    items.Add(CheckBox1, "https://dl.dropboxusercontent.com/something1")
    items.Add(CheckBox2, "https://dl.dropboxusercontent.com/something2")
    items.Add(CheckBox3, "https://dl.dropboxusercontent.com/something3")
    items.Add(CheckBox4, "https://dl.dropboxusercontent.com/something4")
    '..... Add more as required.

    Dim TargetDirectory As String = "C:\"

    Dim ExistingFiles As New List(Of String)

    For Each chk As CheckBox In items.Keys

        If chk.Checked Then
            Dim url As String = items(chk)

            If GetFile(url, TargetDirectory) = False Then
                ExistingFiles.Add(url)
            End If
        End If

    Next

    If ExistingFiles.Count > 0 Then
        Dim msg As String = String.Format("One or more files already exist locally, and have not been downloaded: {0}", String.Join(vbCrLf, ExistingFiles))

        MessageBox.Show(msg, "hi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    End If
End Sub

您可以在表单上添加一个文本框(或类似文件),在其中添加“失败”消息,而不是弹出消息框。如果您这样做,您可以将文本添加到For循环中的文本框中,而不是等到结束。

<强>更新

根据您的评论要求。您似乎有一个与每个文件相关的复选框。您可以通过调整方法来检查选中的值,并将List更改为包含复选框的Dictionary来处理此问题。我已经更新了上面的代码块以适应这个目的。

答案 1 :(得分:1)

从我在代码中看到的内容来看,If语句对于我认为您想要做的事情看起来不正确。

你基本上有第一个If (CheckBox1.Checked = True) Then包装整个代码块。你的其他CheckBox.Checked语句实际上并没有做任何事情。

对于Checkbox2.Checked检查,您的代码如下所示;

If (CheckBox2.Checked = True) Then

    '' do something if true

ElseIf (File.Exists("C:\something3")) Then

    '' regardless of if the CheckBox2 is checked or not it will still go to this check if the file exists

    MessageBox.Show("some message", "hi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else

    '' only if CheckBox2 is not checked then run this code without checking if the file exists

    S3.DownloadFileAsync(New Uri("https://dl.dropboxusercontent.com/something3"), "C:\something3")

End If

'' this next IF block doesnt check for CheckBox2

If (File.Exists("C:\something4")) Then

    MessageBox.Show("some message", "hi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

Else

S4.DownloadFileAsync(New Uri("https://dl.dropboxusercontent.com/something4"), "C:\something4")

End If

我认为您需要从头开始一点一点地开始编写If语句,直到您确定它正常工作,例如。

If CheckBox1.Checked Then
    '' add your sub If statements within
End If

If CheckBox2.Checked Then
    '' add your sub If statements within
End If

您也可以使用

稍微整理一下代码
If CheckBox.Checked Then

而不是

If (CheckBox.Checked = True) Then

Vb.Net不需要括号,CheckBox.Checked也是一个布尔值,所以不需要= True