-
Jun 27th, 2022, 06:14 AM
#1
Thread Starter
Lively Member
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.
-
Jun 27th, 2022, 07:28 AM
#2
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.
-
Jun 27th, 2022, 08:57 AM
#3
Thread Starter
Lively Member
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.
-
Jun 27th, 2022, 09:43 AM
#4
Re: Compare Text
Still way too vague. I also asked specific questions which you ignored.
-
Jun 27th, 2022, 01:18 PM
#5
Re: Compare Text
Originally Posted by StikxX
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?
-
Jun 27th, 2022, 01:23 PM
#6
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.
-
Jun 27th, 2022, 01:33 PM
#7
Thread Starter
Lively Member
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.
-
Jun 27th, 2022, 02:49 PM
#8
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)
-
Jun 27th, 2022, 03:00 PM
#9
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.
-
Jun 27th, 2022, 04:36 PM
#10
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.
-
Jun 27th, 2022, 04:45 PM
#11
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
-
Jun 28th, 2022, 01:04 AM
#12
Thread Starter
Lively Member
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)
-
Jun 28th, 2022, 05:19 AM
#13
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.
-
Jun 28th, 2022, 06:17 AM
#14
Thread Starter
Lively Member
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.
-
Jun 28th, 2022, 03:53 PM
#15
Re: Compare Text
Originally Posted by StikxX
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|