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.
http://img854.imageshack.us/img854/5084/onlythis.png
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.
http://img33.imageshack.us/img33/3563/likethist.png
Thanks.
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:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'String your serching for
Dim SearchTerm As String = "TheWordYouWantToSearchFor"
Dim ResultIndex As Integer = Nothing
Dim SearchStartIndex As Integer = 0
'Loop until the searchword is not found in the RichTextBox
Do Until ResultIndex = -1
'Progressive search (search from the end of the last result)
ResultIndex = RichTextBox1.Find(SearchTerm, SearchStartIndex, RichTextBoxFinds.NoHighlight)
'If the searchstring was not found then cancel the routine
If ResultIndex = -1 Then Exit Do
'Get the line the matched string occurs on (narrows things down a bit)
Dim ResultLine As Integer = RichTextBox1.GetLineFromCharIndex(ResultIndex)
Dim ResultLineStart As Integer = RichTextBox1.GetFirstCharIndexFromLine(ResultLine)
Dim GetWordStart As Integer = ResultLineStart
'Loop backwards from the Index where the match begins to the beginning of the line
For i As Integer = ResultIndex To ResultLineStart Step -1
'Record the position following a space (" ").
If RichTextBox1.Text.Chars(i) = " " Then
GetWordStart = i + 1
Exit For
End If
Next i
'Loop through and find the end of the current Word (index before the next space in the line)
Dim GetWordEnd As Integer = RichTextBox1.TextLength - 1
For i As Integer = ResultIndex To GetWordEnd
If RichTextBox1.Text.Chars(i) = " " Then
GetWordEnd = i
Exit For
End If
Next
'This is the Full 'word' that contains the searchstring
Dim GetFullWord As String = RichTextBox1.Text.Substring(GetWordStart, GetWordEnd - GetWordStart).Trim
'If it starts with a link syntax then select and highlight the text
If GetFullWord.StartsWith("http://") Or GetFullWord.StartsWith("www.") Then
RichTextBox1.Select(GetWordStart, GetWordEnd - GetWordStart)
RichTextBox1.SelectionBackColor = Color.LightBlue
End If
'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
If GetWordEnd = RichTextBox1.TextLength - 1 Then Exit Do Else SearchStartIndex = ResultIndex + 1
Loop
End Sub
End Class
Hope this helps :)
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
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.
Re: How can I highlight links in richtextbox with vb.net?
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 :)
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.
http://s2.postimage.org/knxdwdz0b/how_to_fix_it.png
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 :D