public void ColorVisibleLines(RichTextBox rtfText)
{
int FirstLine = FirstVisibleLine();
int LastLine = LastVisibleLine();
int FirstVisibleChar;
int i = FirstLine;
if ((FirstLine == 0) & (LastLine == 0))
{
return;
}
else
{
while (i < LastLine)
{
FirstVisibleChar = GetCharFromLineIndex(FirstLine);
ColorLineNumber(rtfText, FirstLine, FirstVisibleChar);
FirstLine += 1;
i += 1;
}
}
}
public int FirstVisibleLine()
{
return SendMessage(rtfText.Handle.ToInt32(), (int)EditMessages.GetFirstVisibleLine, 0, 0);
}
public int GetCharFromLineIndex(int LineIndex)
{
return SendMessage(rtfText.Handle.ToInt32(), (int)EditMessages.LineIndex, LineIndex, 0);
}
public int LastVisibleLine()
{
int LastLine = FirstVisibleLine() + (rtfText.Height / rtfText.Font.Height);
if (LastLine > rtfText.Lines.Length | LastLine == 0)
{
LastLine = rtfText.Lines.Length;
}
return LastLine;
}
public void ColorRtb(RichTextBox rtftext)
{
int FirstVisibleChar;
int i = 0;
while (i < rtftext.Lines.Length)
{
FirstVisibleChar = GetCharFromLineIndex(i);
ColorLineNumber(rtftext, i, FirstVisibleChar);
i += 1;
}
}
public void ColorLineNumber(RichTextBox rtfText, int LineIndex, int lStart)
{
string Line = rtfText.Lines[LineIndex].ToLower();
int i = 0;
int Instance;
int SelectionAt = rtfText.SelectionStart;
// Lock the update
LockWindowUpdate(rtfText.Handle.ToInt32());
// Color the line black to remove any previous coloring
rtfText.SelectionStart = lStart;
rtfText.SelectionLength = Line.Length;
rtfText.SelectionColor = Color.Black;
// Find any comments
Instance = Line.IndexOf(";") + 1;
// If there are comments, color them
if (Instance != 0)
{
rtfText.SelectionStart = (lStart + Instance - 1);
rtfText.SelectionLength = (Line.Length - Instance + 1);
rtfText.SelectionColor = Color.Green;
}
if (Instance == 1)
{
// Unlock the update, restore the start and exit
rtfText.SelectionStart = SelectionAt;
rtfText.SelectionLength = 0;
LockWindowUpdate(0);
return; // TODO: might not be correct. Was : Exit Sub
}
// Loop through all the Keywords
while (i < Words.Length)
{
// See if the word is in the Line
Instance = Line.IndexOf(Words[i].Word) + 1;
// If the lines contains the word, color it
if (Instance != 0)
{
rtfText.SelectionStart = (lStart + Instance - 1);
rtfText.SelectionLength = Words[i].Word.Length;
rtfText.SelectionColor = Words[i].Color;
}
i += 1;
}
// Restore the selectionstart
rtfText.SelectionStart = SelectionAt;
rtfText.SelectionLength = 0;
// Unlock the update
LockWindowUpdate(0);
}