2013-07-24 28 views
0

在Visual Studio 2012中使用VB。我無法弄清楚如何清除我需要清除的任何東西。vb(輸出在Richtextbox字段中加倍)

看到我用下面的代碼:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 

    If RichTextBox1.Text > "" Then 
     pingProc.CancelOutputRead() 
     'Need Clearing code here 
     RichTextBox1.Text = "" 
    End If 
    Me.Height = 522 

    With pingProc.StartInfo 
     .FileName = "cmd.exe" 
     .Arguments = "/c C:\Tracert.bat" 
     .RedirectStandardInput = True 
     .RedirectStandardOutput = True 
     .UseShellExecute = False 
     .CreateNoWindow = True 
    End With 

    AddHandler pingProc.OutputDataReceived, AddressOf HandleProcessOutput 

    pingProc.Start() 
    pingProc.BeginOutputReadLine() 

End Sub 

Private Sub HandleProcessOutput(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) 
    Me.Invoke(New DelegateAddText(AddressOf AddText), New Object() {e.Data}) 
End Sub 

Delegate Sub DelegateAddText(ByVal Text As String) 
Private Sub AddText(ByVal Text As String) 
    RichTextBox1.Text &= Text & vbCrLf 
End Sub 

現在,當我運行這段代碼,它運行良好。但我將不得不再次運行它。當我做我最終有兩倍或更多的副本取決於多少次我運行此代碼。

首先運行:

Performing Trace Route to 198.224.169.244 
Tracing route to 244.sub-198-224-169.myvzw.com [198.224.169.244] 
over a maximum of 30 hops: 

第二輪:

Performing Trace Route to 198.224.169.244 
Performing Trace Route to 198.224.169.244 
Tracing route to 244.sub-198-224-169.myvzw.com [198.224.169.244] 
Tracing route to 244.sub-198-224-169.myvzw.com [198.224.169.244] 
over a maximum of 30 hops: 
over a maximum of 30 hops: 

回答

0

魔法門造成Addhandler ......所以,你可以試試這個..

Dim Handled As Boolean 

If Not Handled Then 
    AddHandler pingProc.OutputDataReceived, AddressOf HandleProcessOutput 
    Handled = True 
End If 
+0

感謝您的幫助matzone 。看起來,無論我把它放在我的代碼中的哪個位置,文本現在都會雙倍增加。你有建議我應該把它放在哪裏? – Dam50fifty

+0

@ Dam50fifty ..在類中聲明'handled',並用這個if ... endif塊代替你的addhandler語句。 – matzone