Click here to Skip to main content
15,794,795 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am looking a way to create a HyperLink in a RichTextBox pointing to a line of the text of the same RichTextBox.

I just found how to do that with Internet Links but I don't find a way to do it with the same text inside of the control (like Hyperlinks in MS Word pointing to a header or bookmark).

Thanks in Advance. - CCB

What I have tried:

'Til now I just found how to make hyperlink with internet links not with exact lines of text.
BillWoodruff 7-Feb-16 8:05am    
This is possible using either LinkLabels, or the techniques shown in the CP articles Dave cites here. But, I recommend you not take this on if you are relatively new to C# programming. Tricky stuff to get right, and very easy for the user to destroy your links if they are allowed to edit the contents of the RichTextBox at run-time.
ChrisCreateBoss 7-Feb-16 13:07pm    
I know about LinkLabels, but I want to include the hyperlinks inside the RichTextBox's text, I don't want it outside of the control, 'cause I want the user to be able to create the hyperlink and write which line of the text is going to point those hyperlinks.

This will work as a book index, so if the user clicks one of the hyperlinks (which will be displayed as a list, not listbox) then the richtextbox will scroll down until it finds the line pointed by the hyperlink.
ChrisCreateBoss 7-Feb-16 13:36pm    
In MS Word when you insert a Hyperlink, they give you an option to select if you want it to point a specific place of the document, but they only let you point it to Titles, Bookmarks or the Beginning of the Document. So I want something like that, that I can insert it in my RichTextbox and make it scroll down to a specific place of the text.

1 solution

There is nothing built in that supports this. You'd be creating your own version of a HyperLink complete with the data to identify the place you wanted to go to, a tag of sorts, not a line number.

Line numbers in a RichTextBox are not the visible lines you see on screen. For example, if you have a single line of text with a CR at the end this line can be rendered on screen using any number of lines on screen, though no matter how many lines are on the screen, there is only 1 line of text in the RichTextBox.

There's a couple of articles here on CP that describe something along the lines of what you are. Both have the same title but they are different articles:
Links with Arbitrary Text in a RichTextBox[^]
Links with arbitrary text in a RichTextBox[^]
Share this answer
BillWoodruff 7-Feb-16 8:03am    
My vote of #2:

"There is nothing built in that supports this. You'd be creating your own version of a HyperLink complete with the data to identify the place you wanted to go to, a tag of sorts, not a line number."

Absolutely incorrect. This can be done using either LinkLabels, or using the techniques shown in the first CP article you site.

However, adapting those facilities to do what the OP wants is not trivial, and I would not recommend a beginning programmer to take this on. On the fly modifying of the RTF, or using LinkLabels, is "tricky" to work with, and editing by the end user (if editing is allowed). can easily destroy the links.
ChrisCreateBoss 7-Feb-16 13:19pm    
Those links are good examples on how to create Hyperlinks, but they do not make the function I want.

As I replied to Bill, I want my hyperlinks to point a specific line of the RichTextBox. I can say those hyperlinks will act as a book index in a PDF format(you click one of them, and the document scrolls down until the chapter pointed by the hyperlink is shown).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900