如何将文本从一个文本框显示到另一个文本框

时间:2015-09-19 11:55:17

标签: vb.net cmd textbox

该代码用于使用VB.NET运行CMD。我想做的是,当我输入" ipconfig"然后单击ExecuteButton,会出现很多文本,其中一个单词将是" DNS"。当单词" DNS"已经出现在OutputTextBox中,我希望StatusTextBox显示文本" It Works"。

以下是代码:

Public Class Form1

Private WithEvents MyProcess As Process
Private Delegate Sub AppendOutputTextDelegate(ByVal text As String)



Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Me.AcceptButton = ExecuteButton
    MyProcess = New Process
    With MyProcess.StartInfo
        .FileName = "CMD.EXE"
        .UseShellExecute = False
        .CreateNoWindow = True
        .RedirectStandardInput = True
        .RedirectStandardOutput = True
        .RedirectStandardError = True
    End With
    MyProcess.Start()

    MyProcess.BeginErrorReadLine()
    MyProcess.BeginOutputReadLine()
    AppendOutputText("Process Started at: " & MyProcess.StartTime.ToString)

    If OutputTextBox.Text = "DNS" Then
        StatusTextBox.Text = "It Works"
    End If

End Sub

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    MyProcess.StandardInput.WriteLine("EXIT")
    MyProcess.StandardInput.Flush()
    MyProcess.Close()
End Sub

Private Sub MyProcess_ErrorDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles MyProcess.ErrorDataReceived
    AppendOutputText(vbCrLf & "Error: " & e.Data)
End Sub

Private Sub MyProcess_OutputDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles MyProcess.OutputDataReceived
    AppendOutputText(vbCrLf & e.Data)
End Sub

Private Sub ExecuteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExecuteButton.Click
    MyProcess.StandardInput.WriteLine(InputTextBox.Text)
    MyProcess.StandardInput.Flush()
    InputTextBox.Text = ""
End Sub

Private Sub AppendOutputText(ByVal text As String)
    If OutputTextBox.InvokeRequired Then
        Dim myDelegate As New AppendOutputTextDelegate(AddressOf AppendOutputText)
        Me.Invoke(myDelegate, text)
    Else
        OutputTextBox.AppendText(text)
    End If
End Sub
End Class

以下是一个示例:

http://s7.postimg.org/uj7xcykm1/form1.png

指出我的问题,这是我尝试做的事情:

    If OutputTextBox.Text = "DNS" Then
    StatusTextBox.Text = "It Works"
End If

当我输入" ipconfig"文本" DNS"将显示但StatusTextBox没有任何反应。我做错了什么?

1 个答案:

答案 0 :(得分:0)

您的表单加载事件永远不会等待,因此它会立即检查您的条件语句。您可以将条件语句移动到OutputDataReceived Sub吗?

此外,您的OutputTextBox正在追加数据,因此我无法看到它等于" DNS"。你想看看它吗。包含(" DNS")?

如果您将条件移动到OutputDataReceived,并且您要附加到OutputTextBox,您是否只想查看e.Data.Contains(" DNS")?

- 编辑 -

Private Delegate Sub ChangeStatusTextDelegate(ByVal text As String)

Private Sub MyProcess_OutputDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles MyProcess.OutputDataReceived
    AppendOutputText(vbCrLf & e.Data)

    If e.Data.Contains("DNS") Then
        ChangeStatusText("It works")
    End If
End Sub

Private Sub ChangeStatusText(ByVal text As String)
    If txtStatus.InvokeRequired Then
        Dim myDelegate As New ChangeStatusTextDelegate(AddressOf ChangeStatusText)
        Me.Invoke(myDelegate, text)
    Else
        Me.StatusTextBox.Text = text
    End If
End Sub