Advertisement
Guest User

111

a guest
Oct 18th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. int lastLine = 0;
  2. private void HighlightCurrentLine()
  3. {
  4. // Save current selection
  5. int selectionStart = rtb.SelectionStart;
  6. int selectionLength = rtb.SelectionLength;
  7.  
  8. // Get character positions for the current line
  9. int firstCharPosition = rtb.GetFirstCharIndexOfCurrentLine();
  10. int lineNumber = rtb.GetLineFromCharIndex(firstCharPosition);
  11. int lastCharPosition = rtb.GetFirstCharIndexFromLine(lineNumber + 1);
  12. if (lastCharPosition == -1)
  13. lastCharPosition = rtb.TextLength;
  14.  
  15. // Clear any previous color
  16. if (lineNumber != lastLine)
  17. {
  18. int previousFirstCharPosition = rtb.GetFirstCharIndexFromLine(lastLine);
  19. int previousLastCharPosition = rtb.GetFirstCharIndexFromLine(lastLine + 1);
  20. if (previousLastCharPosition == -1)
  21. previousLastCharPosition = rtb.TextLength;
  22.  
  23. rtb.SelectionStart = previousFirstCharPosition;
  24. rtb.SelectionLength = previousLastCharPosition - previousFirstCharPosition;
  25. rtb.SelectionBackColor = SystemColors.Window;
  26. lastLine = lineNumber;
  27. }
  28.  
  29. // Set new color
  30. rtb.SelectionStart = firstCharPosition;
  31. rtb.SelectionLength = lastCharPosition - firstCharPosition;
  32. if (rtb.SelectionLength > 0)
  33. rtb.SelectionBackColor = Color.PaleTurquoise;
  34.  
  35. // Reset selection
  36. rtb.SelectionStart = selectionStart;
  37. rtb.SelectionLength = selectionLength;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement