Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 显示两个RichTextBox控件之间的差异_Vb.net_Winforms - Fatal编程技术网

Vb.net 显示两个RichTextBox控件之间的差异

Vb.net 显示两个RichTextBox控件之间的差异,vb.net,winforms,Vb.net,Winforms,我试图比较两个richtextbox文本,并将差异显示在第三个richtextbox中。在我对从这个论坛得到的代码做了一些修改之后,它仍然存在一些问题,那就是在我的第三个richtextbox上显示的单词没有什么不同。。。。富文本框的右侧来自文本文件,该文本文件在显示在框中之前已在regex函数中选中 这是用于比较的源代码: Dim txt1(DispBox.Text.Split(" ").Length) As String Dim txt2(DispBox2.Text.Split(" ").L

我试图比较两个richtextbox文本,并将差异显示在第三个richtextbox中。在我对从这个论坛得到的代码做了一些修改之后,它仍然存在一些问题,那就是在我的第三个richtextbox上显示的单词没有什么不同。。。。富文本框的右侧来自文本文件,该文本文件在显示在框中之前已在regex函数中选中

这是用于比较的源代码:

Dim txt1(DispBox.Text.Split(" ").Length) As String
Dim txt2(DispBox2.Text.Split(" ").Length) As String
txt1 = DispBox.Text.Split(" ")
txt2 = DispBox2.Text.Split(" ")
Dim diff1 As String = "" 'Differences between 1 and 2
Dim diff2 As String = "" 'Differences between 2 and 1
Dim diffPosition As Integer ' Set where begin to find and select in RichTextBox

diffPosition = 1 ' Initialize
For Each diff As String In txt1
  If Array.IndexOf(txt2, diff.ToString) = -1 Then
    diff1 += diff.ToString & "  "
    With DispBox
      .Find(diff, diffPosition, RichTextBoxFinds.None) ' Find and select diff in RichTextBox1 starting from position diffPosition in RichtextBox1
      .SelectionFont = New Font(.Font, FontStyle.Bold) ' Set diff in Bold
      .SelectionColor = Color.Blue ' Set diff in blue instead of black
      .SelectionBackColor = Color.Yellow ' highlight in yellow
    End With
  End If
  diffPosition = diffPosition + Len(diff) ' re-Initialize diffPostion to avoid to find and select the same text present more than once
Next

DispBox3.Visible = True
DispBox3.Text = diff1
这是我的上传按钮代码,用来检查regex函数

Dim result As DialogResult = OpenFileDialog1.ShowDialog()

' Test result.
If result = Windows.Forms.DialogResult.OK Then

  ' Get the file name.
  Dim path As String = OpenFileDialog1.FileName
  Try
    ' Read in text.
    Dim text As String = File.ReadAllText(path)
    Dim postupload As String = Regex.Replace(text, "!", "")
    DispBox2.Text = postupload

    ' For debugging.
    Me.Text = text.Length.ToString
  Catch ex As Exception
    ' Report an error.
    Me.Text = "Error"
  End Try
End If
因为在文本文件中,行间将有“!”,我想用“特征线/回车”替换“!”

我的问题是:

  • 为什么“Building&hostname”这个词算作错误的词
  • 为什么在第三RICTEXBOX中显示的新词如果在中线找到,则不在新行中。
  • 其他错误的单词不是颜色,粗体n突出显示

  • 你的代码是基于一个空格分割所有的单词,但是它忽略了换行符,所以它使“running confing building”看起来像一个单词

    试着这样做:

    Dim txt1 As String() = String.Join(" ", DispBox.Lines).Split(" ")
    Dim txt2 As String() = String.Join(" ", DispBox2.Lines).Split(" ")
    

    如果您告诉我们您的两个文本框的内容、您的期望以及您得到的信息,可能会有所帮助。盒子里有什么?换句话说,我们可以使用的邮政编码为我们重现了问题。保持小样本。为了突出显示单词,您需要在应用字体和颜色属性之前选择整个单词。实际上,图片处于“问题”单词中,因为我没有足够的点直接显示图片
    dispbox=左侧的第一个richtextbox
    dispbox2=右侧的第二个richtextbox
    dispbox3=位于“检查”按钮下方的第三个richtextbox,以显示在两个richtextbox中比较后不同的单词
    我想要的是,如果我们说rtb3中的rtb1=hello morning
    rtb2=hello morningg
    应该出现“morning”这个词,因为两个rtb在“morning”中都有不同的

    这就是我想要的。。。希望有帮助~^^嗯,错过了链接的图片。运行你的代码,我只会在第三个文本框中看到“如何”和“首次公开”。耶?为什么我会听到剩下的话?issit问题存在于在rtb2txt1=DispBox.text.Split(“”)txt2=DispBox2.text.Split(“”)中显示之前选中文本文件的正则表达式函数中。issit这两行也需要删除吗?@WMK是的,我用这两行替换了您的txt1和txt2设置。是的,明白了。。。。但这些词并没有进入新行。。。。有什么想法吗?我最不想添加的是一个标签,显示有多少相似性。试着解决它。我得到的一个想法是(total.length.of.the.box-{[diff1/total.length.of.the.box]x100%}正确吗?要把代码放在哪里?@WMK如果你还没有弄明白,你可能需要记录一个关于这个问题的新问题。sry,不是垃圾邮件,而是说,你的意思是用另一个问题或关于新问题的新帖子编辑这个问题?