8

I'm using ICSharpCode.TextEditor.TextEditorControl as my DSL editor. When I get DSL compilation errors, I would like to highlight the offending text to provide a better user experience. However, I'm having difficulty finding how to do this.

So far, I've found that there is a ShowInvalidLines property but I don't see a way to mark any lines as invalid. I also see HighlightSpanStack property of LineSegment and HighlightingStrategy but not sure how they are supposed to be used.

Any help would be appreciated. Thanks!

Igor Pashchuk
  • 2,455
  • 2
  • 22
  • 29

1 Answers1

14

To highlight a piece of text use a TextMarker. The following code underlines the word Error with a red wavy line.

TextEditorControl textEditor = new TextEditorControl();
textEditor.Text = "Mark Error";

int offset = 5;
int length = 5;
TextMarker marker = new TextMarker(offset, length, TextMarkerType.WaveLine, Color.Red);
textEditor.Document.MarkerStrategy.AddMarker(marker);

You can highlight the text with any background and foreground colour and the TextMarkerType supports underlining, wavy lines or a solid block of colour.

Matt Ward
  • 47,057
  • 5
  • 93
  • 94
  • doesnt work on latest 4.4.2 version, would be good if someone could update the syntax. for example, MarketStragety is no more – Bek Raupov Jun 04 '14 at 14:29
  • The last version released of the ICSharpCode.TextEditor was 3.2.1.6466. This question refers to the older ICSharpCode.TextEditor control. AvalonEdit, which is the newer text editor included with SharpDevelop, has a version 4.4.2. AvalonEdit was re-written to support WPF and has a very different API. – Matt Ward Jun 04 '14 at 14:38
  • 1
    I had to put a Refresh() to force the control to redraw. But once I did that, I could see my multiple highlights just fine. – Stuart Nov 16 '21 at 09:33