Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int lastLine = 0;
- private void HighlightCurrentLine()
- {
- // Save current selection
- int selectionStart = rtb.SelectionStart;
- int selectionLength = rtb.SelectionLength;
- // Get character positions for the current line
- int firstCharPosition = rtb.GetFirstCharIndexOfCurrentLine();
- int lineNumber = rtb.GetLineFromCharIndex(firstCharPosition);
- int lastCharPosition = rtb.GetFirstCharIndexFromLine(lineNumber + 1);
- if (lastCharPosition == -1)
- lastCharPosition = rtb.TextLength;
- // Clear any previous color
- if (lineNumber != lastLine)
- {
- int previousFirstCharPosition = rtb.GetFirstCharIndexFromLine(lastLine);
- int previousLastCharPosition = rtb.GetFirstCharIndexFromLine(lastLine + 1);
- if (previousLastCharPosition == -1)
- previousLastCharPosition = rtb.TextLength;
- rtb.SelectionStart = previousFirstCharPosition;
- rtb.SelectionLength = previousLastCharPosition - previousFirstCharPosition;
- rtb.SelectionBackColor = SystemColors.Window;
- lastLine = lineNumber;
- }
- // Set new color
- rtb.SelectionStart = firstCharPosition;
- rtb.SelectionLength = lastCharPosition - firstCharPosition;
- if (rtb.SelectionLength > 0)
- rtb.SelectionBackColor = Color.PaleTurquoise;
- // Reset selection
- rtb.SelectionStart = selectionStart;
- rtb.SelectionLength = selectionLength;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement