2011-10-16 7 views

답변

3
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; 
} 

코드 MSDN

+0

에서 이것은 내가 ... 내가 텍스트를 선택하지 않으 싶지 않았다 정확히 수행 할거야하지만 난 전체 라인을 원하는 강조 표시되고 선택되지 않음. – tr0yspradling