简体   繁体   English

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

[英]how to display text from one textbox into another one

The code is used for using VB.NET to run CMD. 该代码用于使用VB.NET运行CMD。 What I want to do, is when I type in "ipconfig" and click ExecuteButton, a lot of text will appear, and one of those words will be "DNS". 我想做的是,当我键入“ ipconfig”并单击ExecuteButton时,会出现很多文本,其中一个单词是“ DNS”。 When the word "DNS" has appeared in OutputTextBox, I want StatusTextBox to display the text "It Works". 当OutputDNSBox中出现“ DNS”一词时,我希望StatusTextBox显示文本“ It Works”。

Here is the code: 这是代码:

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

Here is an example of what it looks like: 这是一个看起来像的例子:

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

To point out my problem, here is what I try to do: 为了指出我的问题,这是我尝试做的事情:

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

When I do type in "ipconfig" the text "DNS" will show up but nothing happens to StatusTextBox. 当我确实输入“ ipconfig”时,将显示文本“ DNS”,但StatusTextBox没有任何反应。 What am I doing wrong? 我究竟做错了什么?

Your Form Load event is never waiting, so it is checking your conditional statement immediately. 您的Form Load事件永远不会等待,因此它会立即检查您的条件语句。 Can you move the conditional statement to your OutputDataReceived Sub? 您可以将条件语句移动到OutputDataReceived Sub吗?

Also, your OutputTextBox is appending data, so I can't see it ever equaling "DNS". 另外,您的OutputTextBox将附加数据,因此我看不到它等于“ DNS”。 Are you looking to see if it .Contains("DNS")? 您是否正在查看是否包含.DNS()?

And if you move the conditional to OutputDataReceived, and you're appending to the OutputTextBox, do you just want to see if e.Data.Contains("DNS")? 并且,如果将条件移到OutputDataReceived,并且要追加到OutputTextBox,是否只想查看e.Data.Contains(“ DNS”)?

-- edit -- -编辑-

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM