Results 1 to 8 of 8

Thread: How can I highlight links in richtextbox with vb.net?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2011
    Posts
    137

    Cool How can I highlight links in richtextbox with vb.net?

    I can't find out the correct answer how to highlight words in hyperlinks using richtextbox.

    The program which I'm creating is blueskycorp scraper which when type some words in text box and found links to my questions puts in richtextbox like links.

    But the problem I can't find out untill is just how to highlight not words but links in richtextbox to match founded words and highlight them in richtextbox where is extracted links and only I want to mark these links.

    Maybe this you will help you to understand it better seeing in the picture what I want.



    In this example you will see marked words too but this time it is only a text so this is that I want to be highlighted words like this but in links like I have marked.



    Thanks.
    Last edited by polas; Aug 6th, 2011 at 09:27 AM.

  2. #2
    Hyperactive Member
    Join Date
    Apr 2011
    Location
    England
    Posts
    421

    Re: How can I highlight links in richtextbox with vb.net?

    Hi Polas,

    You could use the RichTextBox.Find method to run a progressive search. When a match is found, grab it in a substring by expanding its start and end index to include all characters upto the next/previous space in the sentence. Then you can check the substring to see if it is a link and highlight it as necessary. Here is an example:
    VB.NET Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.  
    5.         'String your serching for
    6.         Dim SearchTerm As String = "TheWordYouWantToSearchFor"
    7.  
    8.         Dim ResultIndex As Integer = Nothing
    9.         Dim SearchStartIndex As Integer = 0
    10.         'Loop until the searchword is not found in the RichTextBox
    11.         Do Until ResultIndex = -1
    12.             'Progressive search (search from the end of the last result)
    13.             ResultIndex = RichTextBox1.Find(SearchTerm, SearchStartIndex, RichTextBoxFinds.NoHighlight)
    14.             'If the searchstring was not found then cancel the routine
    15.             If ResultIndex = -1 Then Exit Do
    16.  
    17.             'Get the line the matched string occurs on (narrows things down a bit)
    18.             Dim ResultLine As Integer = RichTextBox1.GetLineFromCharIndex(ResultIndex)
    19.             Dim ResultLineStart As Integer = RichTextBox1.GetFirstCharIndexFromLine(ResultLine)
    20.             Dim GetWordStart As Integer = ResultLineStart
    21.             'Loop backwards from the Index where the match begins to the beginning of the line
    22.             For i As Integer = ResultIndex To ResultLineStart Step -1
    23.                 'Record the position following a space (" ").
    24.                 If RichTextBox1.Text.Chars(i) = " " Then
    25.                     GetWordStart = i + 1
    26.                     Exit For
    27.                 End If
    28.             Next i
    29.  
    30.             'Loop through and find the end of the current Word (index before the next space in the line)
    31.             Dim GetWordEnd As Integer = RichTextBox1.TextLength - 1
    32.             For i As Integer = ResultIndex To GetWordEnd
    33.                 If RichTextBox1.Text.Chars(i) = " " Then
    34.                     GetWordEnd = i
    35.                     Exit For
    36.                 End If
    37.             Next
    38.  
    39.             'This is the Full 'word' that contains the searchstring
    40.             Dim GetFullWord As String = RichTextBox1.Text.Substring(GetWordStart, GetWordEnd - GetWordStart).Trim
    41.             'If it starts with a link syntax then select and highlight the text
    42.             If GetFullWord.StartsWith("http://") Or GetFullWord.StartsWith("www.") Then
    43.                 RichTextBox1.Select(GetWordStart, GetWordEnd - GetWordStart)
    44.                 RichTextBox1.SelectionBackColor = Color.LightBlue
    45.             End If
    46.  
    47.             'Update the start index for the search to begin from on the next pass, or cancel further searches if we are at the end of the string
    48.             If GetWordEnd = RichTextBox1.TextLength - 1 Then Exit Do Else SearchStartIndex = ResultIndex + 1
    49.  
    50.         Loop
    51.  
    52.     End Sub
    53.  
    54. End Class
    Hope this helps

  3. #3
    Banned
    Join Date
    Mar 2009
    Posts
    764

    Re: How can I highlight links in richtextbox with vb.net?

    as a plan B you could highlight the links with emoticons :
    http://www.vbforums.com/showthread.php?t=648151

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jun 2011
    Posts
    137

    Re: How can I highlight links in richtextbox with vb.net?

    Ok the is good
    But there is the problem with it.

    let's i want to highlight like this

    programming tutorial vb.net and it doesn't highlight.
    It marks links like as programming but not like that and everytime i have to change search string to make it work.

    How to make it work from search box like from textbox and put highlighted links not like programming but Example like programing tutorial vb.net,programming tutorial for beginners vb.net and then hightlight those links.

    and by the way i almost forgot to say that the code in button does not work at all just in richtextbox_changed and one more thing when it hightlights links get hangs up and take a lot of cpu usage.
    How to fix and this.

    Really thank you.

    Thanks again.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jun 2011
    Posts
    137

    Re: How can I highlight links in richtextbox with vb.net?

    And how to remove junk links like these

    http://webcache.googleusercontent.co...=www.google.lt

    i just need to leave these in richtextbox.
    http://www.vbforums.com
    http://www.vbforums.com/showthread.php
    http://www.**********.com
    http://www.dreamingcode.net


    Because i can't remove these junk links like google long links.
    Last edited by polas; Aug 8th, 2011 at 05:32 AM.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jun 2011
    Posts
    137

    Re: How can I highlight links in richtextbox with vb.net?

    Ok i have another my code but it marks only one link and just part of string in yellow color.
    It's ok that that is not haging cpu but i just need that you fix it.
    Just like above yuor code but this time with code can you help me to fix this ?
    And it works great then i will marked like done job

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jun 2011
    Posts
    137

    Re: How can I highlight links in richtextbox with vb.net?

    Code:
    Private Sub RichTextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
            
    Dim index As Integer = Me.RichTextBox1.Find("tutorial") '  
            If index <> -1 Then
                Me.RichTextBox1.Find(RichTextBox1.Text)
                Me.RichTextBox1.SelectionBackColor = Color.Yellow
            End If
     End Sub
    This code give me just apart of string.

    Here is the problem.

    Last edited by polas; Aug 8th, 2011 at 06:03 AM.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jun 2011
    Posts
    137

    Re: How can I highlight links in richtextbox with vb.net?

    Allright guys i found how to highlight links in richtextbox but for your help you can say that is done

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