2011-10-16 6 views

Répondre

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

code de MSDN

+0

Ce fait exactement ce que je disais que je ne voulais pas ... Je ne veux pas le texte sélectionné, mais je veux la ligne ENTIER être mis en évidence, non sélectionné .. – tr0yspradling

Questions connexes