visual studio - Hyperlink to a line of RichTextBox in C# -
i looking way create hyperlink in richtextbox pointing line of text of same richtextbox.
i found how internet links don't find way same text inside of control (it's hyperlinks in ms word pointing header or bookmark).
thanks in advance. - ccb
no, not work unless code necessary stuff yourself.
two suggestions:
- a simple workaround links starting
www. - the nicer solution arbitrary link text
let's have @ both options..:
- using built-in functionality of recognizing url seems right way start, link have look url, not hyperlink anchor.. if can live solution has, say, links this:
www.goto.234, anchors this:#234#rather simple..
a working example can simple this:
private void richtextbox1_linkclicked(object sender, linkclickedeventargs e) { var s = e.linktext.split('.'); string anchor = s[2]; int = richtextbox1.text.indexof("#" + anchor + "#" ); if (a >= 0) richtextbox1.selectionstart = a; else return; // add more checks! richtextbox1.selectionlength = 0; text = anchor + " @ " + a; //richtextbox1.scrolltocaret(); <<--- crashes on machine! // take jump out of click event , works: timer ttt = new timer() { interval = 100 }; ttt.tick += (ss, ee) => { richtextbox1.scrolltocaret(); ttt.stop(); }; } - option two: if you'd rather have more choice of how links should read can this:
start formatting each
- start special character, tilde '~'
- format blue , underlined if want to
- either make 1 word or replace space underlines , format have forecolor equal backcolor
now can job:
public string delimiters = " ()[]{}!&?=/\\,;.\r\n"; private void richtextbox2_click(object sender, eventargs e) { int sstart = -1; string s = getwordat(richtextbox2.text, richtextbox2.selectionstart, delimiters, out sstart); if (s.length < 3) return; string char1 = s.substring(0, 1); if (char1 == "~") { int p = richtextbox2.text.indexof("#" + s.substring(1)); if (p >= 0) { richtextbox2.selectionstart = p; richtextbox2.scrolltocaret(); } } } public static string getwordat(string text, int cursorpos, string delimiters, out int selstart) { int startpos = 0; selstart = startpos; if ((cursorpos < 0) | (cursorpos > text.length) | (text.length == 0)) return ""; if ((text.length > cursorpos) & (delimiters.contains(text[cursorpos]))) return ""; int endpos = text.length - 1; if (cursorpos == text.length) endpos = text.length - 1; else { (int = cursorpos; < text.length; i++) { if (delimiters.contains(text[i])) { endpos = - 1; break; } } } if (cursorpos == 0) startpos = 0; else { (int = cursorpos; > 0; i--) { if (delimiters.contains(text[i])) { startpos = + 1; break; } } } selstart = startpos; return text.substring(startpos, endpos - startpos + 1); } here 2 versions side side, once @ top after clicking on link:
both versions work fine, although both more checks.
note lazy format pseudo-links in second example, show tildes , hashes..
not hard write helper function can insert formatting; search still work searches in text, not rtf property..


Comments
Post a Comment