Results 1 to 15 of 15

Thread: Compare Text

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2022
    Posts
    70

    Compare Text

    Hello,

    how can I check two texts in Richtextbox if they are the same, if not then the differences should be marked red.

    Example:
    Text1: WZNAE
    Text2: 1PWZNAEEU
    Output: 1PWZNAEEU

    Thanks for your help.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Compare Text

    What are the actual rules? You need to be specific. Are you just saying that want to colour characters that appear in the second string but not in the first? The other way too? Something else? Code has to perform SPECIFIC actions so you need to know EACTLY what those actions are. If you don't then you shouldn't even be trying to write code yet, which is probably why you've not be able to do anything at for yourself so far. If you think about what those actions are then you should be able to do something, even if it's got lots of TODOs through it. As it stands, you're just asking us to do your work for you. If this is homework, as I suspect it may be, then that's really not on.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2022
    Posts
    70

    Re: Compare Text

    TextBoxes or Richtextboxes.

    TextBox1: WZNAE
    TextBox2: 1PWZNAEEU

    This TextBox2 is to be compared with TextBox1.
    If TextBox2 does not match TextBox1, then TextBox3 should display it.

    TextBox2 1PWZNAEEU

    It should be displayed which characters are wrong.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Compare Text

    Still way too vague. I also asked specific questions which you ignored.

  5. #5
    Fanatic Member Peter Porter's Avatar
    Join Date
    Jul 2013
    Posts
    532

    Re: Compare Text

    Quote Originally Posted by StikxX View Post
    TextBoxes or Richtextboxes.

    TextBox1: WZNAE
    TextBox2: 1PWZNAEEU

    This TextBox2 is to be compared with TextBox1.
    If TextBox2 does not match TextBox1, then TextBox3 should display it.

    TextBox2 1PWZNAEEU

    It should be displayed which characters are wrong.

    StikxX, is this homework? What have you been taught codewise that your teacher expects you to tweak to solve this problem?

  6. #6
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,195

    Re: Compare Text

    Also, does the order of the text matter??

    In your example the text that matches is in the same order. These are the types of things that jmc is asking you to make clear.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2022
    Posts
    70

    Re: Compare Text

    this is not homework ^^ I would like to use this for myself and to be able to edit a list faster.

    The order is always the same, only at the beginning or end of the text could there be something different.

  8. #8
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: Compare Text

    with indexOf, you will locate the first string in the second and will have the position.

    https://docs.microsoft.com/en-us/dot...ithin-a-string

    when you have the index and the length of the 1st string you just have to change the color of the characters for the 1 one to the index and all the character after the index + the length

    https://www.vbforums.com/showthread....ng-in-text-box

    but you can do that only in a richtextbox

    edit:
    an easier way would be to do the reverse, first color all the text in red and then the located string in black
    Last edited by Delaney; Jun 27th, 2022 at 02:57 PM.
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  9. #9
    Fanatic Member Peter Porter's Avatar
    Join Date
    Jul 2013
    Posts
    532

    Re: Compare Text

    What the hell... I have a little time on my hands.

    I was gonna code this from scratch, but my lazy streak made me Google this, and make minor tweaks.

    This will do what you want, but instead of changing the color of the different text, it changes the color for the same text.

    What you're gonna need is a TextBox, a RichTextBox and a Button.

    When you click the button, all the text changes in the RichTextBox to the color you're gonna use for the different text, then it finds the text you're looking for and changes it to black.

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            RichTextBox1.SelectAll()
            RichTextBox1.SelectionColor = Color.Red
    
            Dim i As Integer = 0
            Dim StopWhile As Boolean = False
    
            While Not StopWhile
    
                Dim j As Integer = RichTextBox1.Text.IndexOf(TextBox1.Text, i)
    
                If j < 0 Then
                    StopWhile = True
                Else
                    RichTextBox1.Select(j, TextBox1.Text.Length)
                    RichTextBox1.SelectionColor = Color.Black
                    i = j + 1
                End If
    
            End While
    
        End Sub
    Last edited by Peter Porter; Jun 27th, 2022 at 05:02 PM.

  10. #10
    Fanatic Member Peter Porter's Avatar
    Join Date
    Jul 2013
    Posts
    532

    Re: Compare Text

    Regular expressions:

    Code:
    Option Strict On
    
    Imports System.Text.RegularExpressions
    
    
    Public Class Form1
    
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            RichTextBox1.SelectAll()
            RichTextBox1.SelectionColor = Color.Red
    
            For Each match As Match In Regex.Matches(RichTextBox1.Text, TextBox1.Text)
                RichTextBox1.Select(match.Index, match.Length)
                RichTextBox1.SelectionColor = Color.Black
            Next
    
        End Sub
    
    
    End Class
    Last edited by Peter Porter; Jun 27th, 2022 at 05:09 PM.

  11. #11
    Fanatic Member Peter Porter's Avatar
    Join Date
    Jul 2013
    Posts
    532

    Re: Compare Text

    Without regular expressions:

    Code:
    Public Class Form1
    
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            RichTextBox1.SelectAll()
            RichTextBox1.SelectionColor = Color.Red
    
            RichTextBox1.Select(RichTextBox1.Text.IndexOf(TextBox1.Text, 0), TextBox1.Text.Length)
            RichTextBox1.SelectionColor = Color.Black
    
        End Sub
    
    
    End Class

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Jan 2022
    Posts
    70

    Re: Compare Text

    Thank you, that works

    I want to copy the text from the RichTextBox to Excel now.
    I paste the text into the clipboard but I can't paste it into Excel.
    But Word works.

    Dim CopyExcel As String = RichTextBox1.Rtf
    Clipboard.SetText(CopyExcel, TextDataFormat.Rtf)

  13. #13
    Fanatic Member Peter Porter's Avatar
    Join Date
    Jul 2013
    Posts
    532

    Re: Compare Text

    You don't need the clipboard to transfer the list to Excel. What you do is convert the contents of the RichTextBox to an Excel worksheet, separating each line to it's own row, and export it to Excel which will open automatically.

    You can find code examples to do this via Google.


    Now if you need to paste the list into specific cells of an Excel workbook, then it's best to open the workbook in a DataGridView, add your list to the correct cells, separating each line to it's own row, and resave.
    Last edited by Peter Porter; Jun 28th, 2022 at 05:47 AM.

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Jan 2022
    Posts
    70

    Re: Compare Text

    I don't want to write directly to a table.
    I want to be able to paste it from the clipboard to X place in an X Excel table.

  15. #15
    Fanatic Member Peter Porter's Avatar
    Join Date
    Jul 2013
    Posts
    532

    Re: Compare Text

    Quote Originally Posted by StikxX View Post
    I don't want to write directly to a table.
    I want to be able to paste it from the clipboard to X place in an X Excel table.
    How many lists are you dealing with? Is the Excel worksheet something you created, or is it from work? What do you mean by X place? A specific location within the Excel worksheet?

    It's best you start a new thread on this so it doesn't get burried with this thread. It would get more attention that way.
    Last edited by Peter Porter; Jun 28th, 2022 at 10:18 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width