none
How can I compare 2 almost same textbox's text in VB.NET? RRS feed

  • Question

  • I'd like to compare them, and show the differences in another textbox.
    Example1:
    Textbox1.Text = "ABC"
    Textbox2.Text = "AB"
    difference in msgbox: "C" is missing

    Example2:
    Textbox1.Text (multiline textbox)=
    "
    ABC
    AGD
    HTD"
    Textbox2.Text(multiline textbox) =
    "
    ABC
    AGD
    JTZ"

    difference in msgbox: "HTD" is missing And JTZ is plus

     How should I do?

    Friday, November 16, 2018 3:10 PM

Answers

  • Perhaps this is what you want?

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Console.WriteLine("TextBox1 -> TextBox2")
            Dim results1 = TextBox1.Lines.Except(TextBox2.Lines).ToArray
            For Each value As String In results1
                Console.WriteLine(value)
            Next
            Console.WriteLine("TextBox2 -> TextBox1")
            Dim results2 = TextBox2.Lines.Except(TextBox1.Lines).ToArray
            For Each value As String In results2
                Console.WriteLine(value)
            Next
        End Sub
    End Class
    

    Results

    TextBox1 -> TextBox2
    HTD
    TextBox2 -> TextBox1
    JTZ


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by barnus2013 Friday, November 16, 2018 7:55 PM
    Friday, November 16, 2018 7:09 PM
    Moderator

All replies

  • Hi

    Please show your code so far and any problems with it.


    Regards Les, Livingston, Scotland

    Friday, November 16, 2018 3:39 PM
  • Perhaps this is what you want?

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Console.WriteLine("TextBox1 -> TextBox2")
            Dim results1 = TextBox1.Lines.Except(TextBox2.Lines).ToArray
            For Each value As String In results1
                Console.WriteLine(value)
            Next
            Console.WriteLine("TextBox2 -> TextBox1")
            Dim results2 = TextBox2.Lines.Except(TextBox1.Lines).ToArray
            For Each value As String In results2
                Console.WriteLine(value)
            Next
        End Sub
    End Class
    

    Results

    TextBox1 -> TextBox2
    HTD
    TextBox2 -> TextBox1
    JTZ


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by barnus2013 Friday, November 16, 2018 7:55 PM
    Friday, November 16, 2018 7:09 PM
    Moderator