触发richtextbox textchanged事件,直到它检测到某些文本

时间:2015-08-08 01:44:40

标签: vb.net richtextbox textchanged

我正在处理可以发送和接收某些命令的serialport。我想实现一个重试功能,允许我(客户端)重新发送数据,直到收到设备(服务器)并向我发送响应。

因此我创建了一个简单的代码来说明这种功能。

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
       RichTextBox1.AppendText(Environment.NewLine & "Sample")
    End Sub

    Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged
       Console.WriteLine("Trigger textchanged")

       Dim totalLines As Integer = Me.RichTextBox1.Lines.Length
       Dim lastLine As String = Me.RichTextBox1.Lines(totalLines - 1)

       Dim CSTAT_Check As Boolean = lastLine Like "*Sample*"
       If CSTAT_Check = True Then
           RichTextBox1.AppendText(Environment.NewLine & "Sample")
       End If
    End Sub
End Class

它的工作方式是这样的,我会点击按钮将sample字符串附加到richtextbox,然后richtextbox textchange_event将被触发,导致它重新发送将sample字符串转换为本身,并将其导致trigger另一个textchange_event,依此类推,直到设备收到sample字符串为止作为回报,设备(服务器)将向我的设备(客户端)发送sample_accepted字符串,因为textchanged_event没有检测到richtextbox最后一行中的sample字符串,它将不再发送另一个sample字符串到richtextbox

这有点难以理解所以我将创建一个简单的图表

客户(我) 服务器(设备)

发送sample字符串Doesn't detected

发送sample字符串 again Doesn't detected again

发送sample字符串 again Doesn't detected again

发送sample字符串 again Doesn't detected again

发送sample字符串 again Detected sample send sample_accepted

客户端 no longer 发送sample字符串,因为服务器已经检测到它。

我的代码中的问题似乎在第一次触发后它不会再次触发textchanged_event

1 个答案:

答案 0 :(得分:0)

??? 如果更改响应该属性的代码中的属性,则不会触发另一个更改的事件。 ???您需要在进行更改后手动触发textchanged事件。

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
       RichTextBox1.AppendText(Environment.NewLine & "Sample")
    End Sub

    Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged
       Console.WriteLine("Trigger textchanged")

       Dim totalLines As Integer = Me.RichTextBox1.Lines.Length
       Dim lastLine As String = Me.RichTextBox1.Lines(totalLines - 1)

       Dim CSTAT_Check As Boolean = lastLine Like "*Sample*"
       If CSTAT_Check = True Then
           RichTextBox1.AppendText(Environment.NewLine & "Sample")
           RichTextBox1_TextChanged(sender, New EventArgs())
       End If
    End Sub
End Class