1. /// <summary>
  2. /// This additional code block checks the locations of links
  3. /// and desc. it via a string which contains informations of how many links are there
  4. /// .Split('&')-1 and the select information .Select(.Split('&')[i].Split('-')[0],.Split('&')[i].Split('-')[1])
  5. /// After we select the links we can SetSelectionLink(true) to get our links back.
  6. /// </summary>
  7. public string getLinkPositions()
  8. {
  9. string pos = "";
  10. for (int i = 0; i < this.TextLength; i++)
  11. {
  12. this.Select(i, 1);
  13. int isLink = GetSelectionLink();
  14. if (isLink == 1)
  15. {
  16. //the selected first character is a part of link, now find its last character
  17. for (int j = i + 1; j <= this.TextLength; j++)
  18. {
  19. this.Select(j, 1);
  20. isLink = GetSelectionLink();
  21. if (isLink != 1 || j == this.TextLength)
  22. {
  23. //we found the last character's +1 so end char is (j-1), start char is (i)
  24. pos += (i) + "-" + ((j - 1) - (i - 1)) + "&"; //j-1 to i but i inserted -1 one more so we can determine the right pos
  25. i = j; //cont. from j+1
  26. break; //exit second for cont. from i = j+1 (i will increase on new i value)
  27. }
  28. }
  29. }
  30. }
  31. this.DeselectAll();
  32. return pos;
  33. }
  34. /// <summary>
  35. /// This method generates the links back only created via InsertLink(string text)
  36. /// and overloaded InsertLink(string text,int position)
  37. /// </summary>
  38. /// <param name="pos">the pos string from getLinkPositions</param>
  39. public void setLinkPositions(string pos)
  40. {
  41. string[] positions = pos.Split('&');
  42. for (int i = 0; i < positions.Length - 1; i++)
  43. {
  44. string[] xy = positions[i].Split('-');
  45. this.Select(Int32.Parse(xy[0]), Int32.Parse(xy[1]));
  46. this.SetSelectionLink(true);
  47. this.Select(Int32.Parse(xy[0]) + Int32.Parse(xy[1]), 0);
  48. }
  49. this.DeselectAll();
  50. }

http://www.codeproject.com/Articles/9196/Links-with-arbitrary-text-in-a-RichTextBox

improvement