Find and Highlight Words - jacobslusser/ScintillaNET GitHub Wiki
The following example will find all occurrences of the string specified (case-insensitive) and highlight them with a light-green indicator:
private void HighlightWord(string text)
{
if (string.IsNullOrEmpty(text))
return;
// Indicators 0-7 could be in use by a lexer
// so we'll use indicator 8 to highlight words.
const int NUM = 8;
// Remove all uses of our indicator
scintilla.IndicatorCurrent = NUM;
scintilla.IndicatorClearRange(0, scintilla.TextLength);
// Update indicator appearance
scintilla.Indicators[NUM].Style = IndicatorStyle.StraightBox;
scintilla.Indicators[NUM].Under = true;
scintilla.Indicators[NUM].ForeColor = Color.Green;
scintilla.Indicators[NUM].OutlineAlpha = 50;
scintilla.Indicators[NUM].Alpha = 30;
// Search the document
scintilla.TargetStart = 0;
scintilla.TargetEnd = scintilla.TextLength;
scintilla.SearchFlags = SearchFlags.None;
while (scintilla.SearchInTarget(text) != -1)
{
// Mark the search results with the current indicator
scintilla.IndicatorFillRange(scintilla.TargetStart, scintilla.TargetEnd - scintilla.TargetStart);
// Search the remainder of the document
scintilla.TargetStart = scintilla.TargetEnd;
scintilla.TargetEnd = scintilla.TextLength;
}
}
This example also illustrates the "set-once, run-many" style API that Scintilla is known for. When performing a search, the TargetStart and TargetEnd properties are set to indicate the search range prior to calling SearchInTarget. The indicators API is similar. The IndicatorCurrent property is first set and then subsequent calls to IndicatorClearRange and IndicatorFillRange make use of that value.
NOTE: Indicators and styles can be used simultaneously.