|
-
Aug 6th, 2011, 09:24 AM
#1
Thread Starter
Addicted Member
-
Aug 6th, 2011, 02:21 PM
#2
Hyperactive Member
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
-
Aug 6th, 2011, 03:43 PM
#3
Banned
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
-
Aug 8th, 2011, 02:51 AM
#4
Thread Starter
Addicted Member
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.
-
Aug 8th, 2011, 05:27 AM
#5
Thread Starter
Addicted Member
Re: How can I highlight links in richtextbox with vb.net?
Last edited by polas; Aug 8th, 2011 at 05:32 AM.
-
Aug 8th, 2011, 05:57 AM
#6
Thread Starter
Addicted Member
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
-
Aug 8th, 2011, 05:57 AM
#7
Thread Starter
Addicted Member
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.
-
Aug 8th, 2011, 01:28 PM
#8
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|