1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace WindowsApplication5
{
public class TRTBPlus : RichTextBox
{
private bool EnableEventSelectionChanged; //évite la récursivité sans fin dans HighlightCurrentLine quand il est appelé dans TRTBPlus_SelectionChanged
private int OldLineNr; //ancienne ligne sélectionnée pour la déselectionner
public TRTBPlus()
: base()
{
EnableEventSelectionChanged = true;
SelectionChanged += new EventHandler(TRTBPlus_SelectionChanged);
}
void TRTBPlus_SelectionChanged(object sender, EventArgs e)
{
HighlightCurrentLine();
}
/// <summary>
/// Surcharge spéciale de la fonction LoadFile qui met en évidence la première ligne
/// d'un fichier à son ouverture. Le type de texte est forcé à PlainText
/// </summary>
/// <param name="path">Chemin du nouveau fichier</param>
public new void LoadFile(string path)
{
LoadFile(path, RichTextBoxStreamType.PlainText);
OldLineNr = -1;
HighlightCurrentLine();
}
private void HighlightCurrentLine()
{
int NoLine;
int FirstCar;
Color OldCol;
int OldSelStart;
if (EnableEventSelectionChanged) //pour éviter une récursivité dans TRTBPlus_SelectionChanged
{
FirstCar = GetFirstCharIndexOfCurrentLine();
NoLine = GetLineFromCharIndex(FirstCar);
if (NoLine != OldLineNr)
{
EnableEventSelectionChanged = false;
OldSelStart = SelectionStart;
OldCol = Color.FromKnownColor(KnownColor.Window);
//déselection de l'ancienne ligne
if (OldLineNr > -1)
{
SelectionStart = GetFirstCharIndexFromLine(OldLineNr);
SelectionLength = Lines[OldLineNr].Length;
SelectionBackColor = OldCol;
SelectionLength = 0;
}
SelectionStart = FirstCar;
SelectionLength = Lines[NoLine].Length;
SelectionBackColor = Color.FromKnownColor(KnownColor.ActiveBorder);
DeselectAll();
SelectionStart = OldSelStart; //remise du curseur à son ancienne position
OldLineNr = NoLine;
EnableEventSelectionChanged = true;
}
}
}
}
} |
Partager