-
Jun 28th, 2016, 09:22 AM
#1
Thread Starter
Lively Member
Removing the color after moving next?
Is there a way to remove the highlighted text once you select the next item? Here is a rough code, but it doesn't reset the color. What I observed is that one cannot pinpoint which word is being highlighted next because the previous words are highlighted! So, I thought maybe there should be one way to reset the color once you move to the next item to allow the user to know which word has been highlighted next! Can you help with this? If the search word highlights part of that phrase again that was previously highlighted, I want the user to know which part of that phrase is going to be replace.
Code:
If foundIndex >= startZ Then
ContextMenuStrip1.Items.Clear()
For Each replacement In replacements(checkWord)
ContextMenuStrip1.Items.Add(replacement.ToLowerInvariant, Nothing, Sub(sender As Object, e As EventArgs)
RichTextBox1.SelectedText = GetWordWithOutBracketedText(John)
End Sub)
Next
ContextMenuStrip1.Show(RichTextBox1, RichTextBox1.GetPositionFromCharIndex(RichTextBox1.SelectionStart + RichTextBox1.SelectionLength))
endindex = checkWord.Length
RichTextBox1.Select(foundIndex, endindex)
RichTextBox1.SelectionColor = Color.Blue
End If
-
Jun 28th, 2016, 11:35 AM
#2
Re: Removing the color after moving next?
You set the colour using this:
Code:
RichTextBox1.SelectionColor = Color.Blue
...so to set it back again (assuming you know it will always be the same colour), you can use almost the same code, but do it before altering the selection, eg:
Code:
RichTextBox1.SelectionColor = Color.Black 'assuming you want Black
RichTextBox1.Select(foundIndex, endindex)
RichTextBox1.SelectionColor = Color.Blue
Note that it may be better to have that line earlier in the routine (possibly before the If).
If you can't be certain what the "old" colour(s) will be, you will need to do extra work to store the previous colour(s) (just before setting it to Blue), and restore the "old" colour(s) where I have suggested Black.
-
Jun 28th, 2016, 12:17 PM
#3
Re: Removing the color after moving next?
I tend to use the ForeColor property when setting back to the default color or remove previous selection.
VB.NET Code:
RichTextBox1.SelectionColor = RichTextBox1.ForeColor
-
Jun 29th, 2016, 06:14 AM
#4
Thread Starter
Lively Member
Re: Removing the color after moving next?
OK, will see if it works then I'll get back to you!
-
Jul 4th, 2016, 10:07 AM
#5
Thread Starter
Lively Member
Re: Removing the color after moving next?
It refused to work when I tested the code. How do you resolve this?
-
Jul 4th, 2016, 10:21 AM
#6
Thread Starter
Lively Member
Resetting the color of highlighted text on a richtextbox
I asked a question, but I did not get an answer on how to reset the color of a highlighted word on this code. How do you do it?
Code:
If foundIndex >= startZ Then
ContextMenuStrip1.Items.Clear()
For Each replacement In replacements(checkWord)
ContextMenuStrip1.Items.Add(replacement.ToLowerInvariant, Nothing, Sub(sender As Object, e As EventArgs)
RichTextBox1.SelectedText = GetWordWithOutBracketedText(John)
End Sub)
Next
ContextMenuStrip1.Show(RichTextBox1, RichTextBox1.GetPositionFromCharIndex(RichTextBox1.SelectionStart + RichTextBox1.SelectionLength))
endindex = checkWord.Length
RichTextBox1.Select(foundIndex, endindex)
RichTextBox1.SelectionColor = Color.Blue
End If
-
Jul 4th, 2016, 10:36 AM
#7
Re: Removing the color after moving next?
It refused to work when I tested the code. How do you resolve this?
Have you applied si_the_geek's suggestion? If yes, do show us the updated code.
-kgc
-
Jul 4th, 2016, 11:01 AM
#8
Re: Resetting the color of highlighted text on a richtextbox
Originally Posted by nqioweryuadfge
I asked a question, but I did not get an answer on how to reset the color of a highlighted word on this code. How do you do it?
Try saving the selected stuff to some variables before you do the coloring so you can restore them. If coloring multiple words, colors, etc, then I'd make a sclass for those properties anfd then make a list of that class to store that information.
Quick idea I slapped together in about 5 mins, Click Button1 to color the words "do" to red, then click button2 to restore the words to whatever they were before changing them to red.
Code:
Public Class Form1
' class for colored word info
Private Class ColorWord
Public wordStart As Integer
Public wordLength As Integer
Public wordColor As Color
End Class
' list for restoring colored words
Private coloredWords As New List(Of ColorWord)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' add some text to RTB for testing
RichTextBox1.Text = "How do you do it?"
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' color and save for restoring
Dim WordSearch = "do" ' the word/char to color
Dim WordColor = Color.Red ' forecolor to use
' find and color all matching words.
Dim FindPosition As Integer = RichTextBox1.Find(WordSearch, 0, RichTextBoxFinds.WholeWord)
While FindPosition <> -1
' save word info for restoring BEFORE coloring!
Dim cw As New ColorWord
With cw
.wordStart = FindPosition ' save word start
.wordLength = WordSearch.Length ' save word length
.wordColor = RichTextBox1.SelectionColor ' save word color
End With
coloredWords.Add(cw)
' color the word!
RichTextBox1.SelectionColor = WordColor
' find next match
If FindPosition + WordSearch.Length < RichTextBox1.TextLength Then
FindPosition = RichTextBox1.Find(WordSearch, FindPosition + WordSearch.Length, RichTextBox1.TextLength, RichTextBoxFinds.WholeWord)
Else
Exit While
End If
End While
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
RestoreAllColors() ' restore previous colors
End Sub
Private Sub RestoreAllColors()
' restore previous colors
For Each word In coloredWords
RichTextBox1.SelectionStart = word.wordStart
RichTextBox1.SelectionLength = word.wordLength
RichTextBox1.SelectionColor = word.wordColor
Next
' since all items restored just clear the list.
coloredWords.Clear()
End Sub
End Class
-
Jul 4th, 2016, 11:02 AM
#9
Re: Resetting the color of highlighted text on a richtextbox
Originally Posted by nqioweryuadfge
I asked a question, but I did not get an answer on how to reset the color of a highlighted word on this code. How do you do it?
You had an answer nearly a week ago (soon after you posted it), and don't seem to have applied the suggestion.
Please do not post multiple threads for the same question (original thread here).
This thread is now closed.
edit: bad timing, due to Edgemeal's post while I was posting, I've merged the threads
Last edited by si_the_geek; Jul 4th, 2016 at 11:08 AM.
-
Jul 6th, 2016, 03:15 AM
#10
Thread Starter
Lively Member
Re: Resetting the color of highlighted text on a richtextbox
Edgemeal
I will get back to you.
-
Jul 6th, 2016, 03:16 AM
#11
Thread Starter
Lively Member
Re: Removing the color after moving next?
KGComputers
Si_the_geek's suggestion never worked! Let me work on Edgemeal's first then I will get back to you.
Last edited by nqioweryuadfge; Jul 6th, 2016 at 03:25 AM.
Tags for this Thread
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
|