Results 1 to 13 of 13

Thread: Highlighting text

  1. #1

    Thread Starter
    Hyperactive Member leeprice2006's Avatar
    Join Date
    Jul 2006
    Posts
    258

    Highlighting text

    Im using command to find a string in a rich text box:

    Code:
            RichTextBox1.Find(TextBox1.Text)
    How do I highlight the text after it has been found?
    Hello, Im a complete beginner so I if I ask obvious questions don't laugh. I have had no special training and I thought I'd learn and I thought this is the best place to start.

    I started off using VB 6.0 but when I bought a new computer I decided to upgrade to VB 2005, so its a little bit different to what i'm used to

  2. #2
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Highlighting text

    vb.net Code:
    1. Me.RichTextBox1.SelectionBackColor = Color.Blue

  3. #3

    Thread Starter
    Hyperactive Member leeprice2006's Avatar
    Join Date
    Jul 2006
    Posts
    258

    Re: Highlighting text

    how would I remove the highlightin when the text is changed
    Hello, Im a complete beginner so I if I ask obvious questions don't laugh. I have had no special training and I thought I'd learn and I thought this is the best place to start.

    I started off using VB 6.0 but when I bought a new computer I decided to upgrade to VB 2005, so its a little bit different to what i'm used to

  4. #4
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Highlighting text

    This would highlight the found text
    vb Code:
    1. Private Sub FindButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindButton.Click
    2.         Dim index As Integer = Me.RichTextBox1.Text.IndexOf(Me.FindTextBox.Text)
    3.         Me.RichTextBox1.SelectionStart = index
    4.         Me.RichTextBox1.SelectionLength = Me.FindTextBox.Text.Length
    5.         Me.RichTextBox1.Focus()
    6.     End Sub
    You also can change its backcolor
    vb Code:
    1. Private Sub FindButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindButton.Click
    2.         Dim index As Integer = Me.RichTextBox1.Text.IndexOf(Me.FindTextBox.Text)
    3.         Me.RichTextBox1.SelectionStart = index
    4.         Me.RichTextBox1.SelectionLength = Me.FindTextBox.Text.Length
    5.         Me.RichTextBox1.SelectionBackColor = Color.Yellow
    6.     End Sub
    and the forcolor
    vb Code:
    1. Private Sub FindButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindButton.Click
    2.         Dim index As Integer = Me.RichTextBox1.Text.IndexOf(Me.FindTextBox.Text)
    3.         Me.RichTextBox1.SelectionStart = index
    4.         Me.RichTextBox1.SelectionLength = Me.FindTextBox.Text.Length
    5.         Me.RichTextBox1.SelectionColor = Color.Red
    6.     End Sub

  5. #5
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: Highlighting text

    To highlight/unhighlight, just use the same code as VBDT posted;

    Code:
        'Highlight selected text
        Private Sub HighlightSelection_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles HighlightSelection.Click
            RichTextBox1.SelectionColor = Color.Blue
        End Sub
    
        'UnHighlight selected text
        Private Sub UnHighlightSelection_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles UnHighlightSelection.Click
            RichTextBox1.SelectionColor = Me.ForeColor
        End Sub
    
        'Clear all highlighted text
        Private Sub UnHighlight_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles UnHighlight.Click
            RichTextBox1.SelectAll()
            RichTextBox1.SelectionColor = Me.ForeColor
            RichTextBox1.DeselectAll()
        End Sub
    Last edited by Bulldog; Aug 11th, 2007 at 07:16 PM.

  6. #6
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142

    Re: Highlighting text

    why are people giving such lengthy codes for this?
    the RichTextBox has a setting in the properties window called HideSelection it's default is True ( so as not to highlight )
    Just set that to False , then
    Code:
    RichTextBox1.Find(TextBox1.Text)
    will highlight the text it finds
    you can set HideSelection through code also like this...
    Code:
            RichTextBox1.HideSelection = False
            RichTextBox1.Find(TextBox1.Text)
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  7. #7

    Thread Starter
    Hyperactive Member leeprice2006's Avatar
    Join Date
    Jul 2006
    Posts
    258

    Re: Highlighting text

    Thanks! is there a way so that when the user clicks the button again it will highlight the next one found
    Hello, Im a complete beginner so I if I ask obvious questions don't laugh. I have had no special training and I thought I'd learn and I thought this is the best place to start.

    I started off using VB 6.0 but when I bought a new computer I decided to upgrade to VB 2005, so its a little bit different to what i'm used to

  8. #8
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142

    Re: Highlighting text

    yes buddy, you can expand on the Find function
    Code:
    '/// find first instance ( only matching whole words )
    RichTextBox1.Find(TextBox1.Text, RichTextBoxFinds.WholeWord)
    then find next item ...
    Code:
    '/// specify the index to start searching from ( anything above the start of the current selection will do even RichTextBox1.SelectionStart + 1 )
            RichTextBox1.Find(TextBox1.Text, RichTextBox1.SelectionStart + RichTextBox1.SelectionLength, RichTextBoxFinds.WholeWord)
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  9. #9

    Thread Starter
    Hyperactive Member leeprice2006's Avatar
    Join Date
    Jul 2006
    Posts
    258

    Re: Highlighting text

    Nothings happening
    Hello, Im a complete beginner so I if I ask obvious questions don't laugh. I have had no special training and I thought I'd learn and I thought this is the best place to start.

    I started off using VB 6.0 but when I bought a new computer I decided to upgrade to VB 2005, so its a little bit different to what i'm used to

  10. #10
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142

    Re: Highlighting text

    are you clicking a different button than the one to select the 1st word? you aint clicking the same button?
    also have you tried RichTextBox1.Find(TextBox1.Text, " position to search from " ) without the last parameter?
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  11. #11
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142

    Re: Highlighting text

    i put 2 buttons on a form, one to search for the 1st instance of the word, the 2nd button to find subsequent instances of the same word. it works fine here. this is all i did ...
    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            RichTextBox1.HideSelection = False
            RichTextBox1.Find(TextBox1.Text, RichTextBoxFinds.WholeWord)
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            '/// with RichTextBox1.HideSelection set to False
            '/// this searches for subsequent instances of the same word & highlights them.
            RichTextBox1.Find(TextBox1.Text, RichTextBox1.SelectionStart + RichTextBox1.SelectionLength, RichTextBoxFinds.WholeWord)
        End Sub
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  12. #12
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Highlighting text

    Quote Originally Posted by dynamic_sysop
    why are people giving such lengthy codes for this?
    the RichTextBox has a setting in the properties window called HideSelection it's default is True ( so as not to highlight )
    Just set that to False , then
    Code:
    RichTextBox1.Find(TextBox1.Text)
    will highlight the text it finds
    you can set HideSelection through code also like this...
    Code:
            RichTextBox1.HideSelection = False
            RichTextBox1.Find(TextBox1.Text)
    I tested your code and it highlights the word but the RichTextBox box blinks when I click on the button. Do you think that is normal? So what is the advantage of your code?

    I think it would be better to set the HideSelection property in design time so that it won’t blink at run time.

  13. #13
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142

    Re: Highlighting text

    yes it's very true that it's best to set HideSelection @ design time.
    I was only showing the function is there by running it under a button click that's why i said to do so in my 1st post
    RichTextBox has a setting in the properties window called HideSelection it's default is True ( so as not to highlight )
    Just set that to False , then
    the advantage of the Find method & the HideSelection property is that that's what they are there for. should be fine with the HideSelection set at design in the properties window.
    i spent a few years making chatroom bots for msn chat ( until they closed it ) through VB5 to VB6, then on to the .NET / C#, this gave me a great insight into the Richtextbox, due to the amount of work i had to do with them.
    Regards.
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

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