Results 1 to 11 of 11

Thread: Removing the color after moving next?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    114

    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

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    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.

  3. #3
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    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:
    1. RichTextBox1.SelectionColor = RichTextBox1.ForeColor
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    114

    Re: Removing the color after moving next?

    OK, will see if it works then I'll get back to you!

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    114

    Re: Removing the color after moving next?

    It refused to work when I tested the code. How do you resolve this?

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    114

    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

  7. #7
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    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
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  8. #8
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Resetting the color of highlighted text on a richtextbox

    Quote Originally Posted by nqioweryuadfge View Post
    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

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Resetting the color of highlighted text on a richtextbox

    Quote Originally Posted by nqioweryuadfge View Post
    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.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    114

    Re: Resetting the color of highlighted text on a richtextbox

    Edgemeal

    I will get back to you.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    114

    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
  •  



Click Here to Expand Forum to Full Width