2011-10-16 74 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