2013-02-07 46 views
0

我遇到了一个问题,如果我多次运行我的操作,我在同一个项目的ListView中获得多个条目。VB.net ListView多次添加项目

我正在创建一个简单的网络扫描器/主机名抓取器,将项目添加到列表视图,因为他们活着回来我的ping测试。

当我第一次运行它时,它运行良好,并创建一个条目,因为它应该。

当我运行它的后续时间,它创建的项目多次,因为我已经跑了代码前。第三次击打开始时,每创建一次,每次输入3次。

这是我走按钮代码:

Private Sub Go_Click(sender As Object, e As EventArgs) Handles Go.Click 
     Dim verifyIP 
     ListView1.Items.Clear() 
     chkDone = 0 
     verifyIP = ipChk(ipAdd.Text) 
     If verifyIP = 1 Then 
      ipAddy = Split(ipAdd.Text, ".") 
      pingTest1.WorkerReportsProgress = True 
      pingTest1.WorkerSupportsCancellation = False 
      AddHandler pingTest1.ProgressChanged, AddressOf pingTest1_ProgressChanged 
      pingTest1.RunWorkerAsync() 
      pingTest2.WorkerReportsProgress = True 
      pingTest2.WorkerSupportsCancellation = False 
      AddHandler pingTest2.ProgressChanged, AddressOf pingTest2_ProgressChanged 
      pingTest2.RunWorkerAsync() 
      pingTest3.WorkerReportsProgress = True 
      pingTest3.WorkerSupportsCancellation = False 
      AddHandler pingTest3.ProgressChanged, AddressOf pingTest3_ProgressChanged 
      pingTest3.RunWorkerAsync() 
      pingTest4.WorkerReportsProgress = True 
      pingTest4.WorkerSupportsCancellation = False 
      AddHandler pingTest4.ProgressChanged, AddressOf pingTest4_ProgressChanged 
      pingTest4.RunWorkerAsync() 
      While chkDone < 4 
       wait(25) 
      End While 
     Else 
      MsgBox("IP Invalid") 
     End If 
     MsgBox("Done") 
    End Sub 

这里是后台工作人员之一,我现在用的是代码:

Private WithEvents pingTest1 As BackgroundWorker = New BackgroundWorker 

    Private Sub pingTest1_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles pingTest1.DoWork 
     Try 
      Dim hostCheck 
      pingResult1 = 0 
      pingTestDone1 = 0 
      tryIP1 = ipAddy(0) & "." & ipAddy(1) & "." & ipAddy(2) & ".1" 
      If My.Computer.Network.Ping(tryIP1) = True Then 
       'Dim pingsender As New Net.NetworkInformation.Ping 
       'If pingsender.Send(tryIP).Status = Net.NetworkInformation.IPStatus.Success Then 
       Try 
        'Dim host As System.Net.IPHostEntry 
        hostCheck = "" 
        'host = System.Net.Dns.GetHostByAddress(tryIP3) 
        'MsgBox(host.HostName) 
        'host3 = host.HostName 
        'hostCheck = System.Net.Dns.GetHostEntry(tryIP3).HostName 
        hostCheck = System.Net.Dns.GetHostByAddress(tryIP1) 
        'get the hostname property 
        hostCheck = hostCheck.HostName 
        pingTest1.ReportProgress("1", hostCheck) 
       Catch f As Exception 
        'MsgBox("Error: " & f.Message) 
        pingTest1.ReportProgress("1", "No Hostname Found") 
       End Try 
      Else 
       pingResult1 = 2 
      End If 
     Catch d As Exception 
      MsgBox("There was an error trying to ping the IP Address: " & d.Message) 
     End Try 
    End Sub 

    Private Sub pingTest1_ProgressChanged(e.ByVal sender As Object, ByVal e As ProgressChangedEventArgs) 
     MsgBox("Hey") 
     Dim str(2) As String 
     Dim itm As ListViewItem 
     str(0) = tryIP1 & " Is Alive!!!" 
     str(1) = e.UserState 
     itm = New ListViewItem(str) 
     ListView1.Items.Add(itm) 
     str(0) = "" 
     str(1) = "" 
     itm = Nothing 
    End Sub 

Private Sub pingTest1_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles pingTest1.RunWorkerCompleted 
    chkDone = chkDone + 1 
End Sub 
  • 我加入了嘿框和足够的肯定ProgressChanged事件触发了我点击Go按钮的次数。这是我编码不正确的东西吗?

回答

1

这很可能是因为您正在添加,但未删除处理程序以更改进度,因此您要多次处理该事件。

尝试在实例化后台工作人员时添加进度更改事件处理程序,而不是每次单击按钮时。这样他们只会处理一次。

+0

这解决了它,永远不会想到这一点。非常感激。 – user2048863

+0

没有问题! – PGallagher