New to Telerik UI for WinFormsStart a free 30-day trial

Add New Keyword to Existing Tagger

Updated over 6 months ago

Environment

Product VersionProductAuthor
2023.1.314RadSyntaxEditor for WinFormsDinko Krastev

Description

The RadSyntaxEditor control provides a set of predefined taggers to style specific words in the document. In this article, we will demonstrate how we can add keywords to a predefined tagger without creating a custom tagger from scratch. In the following image, you can observe how the Telerik word is marked by the tagger.

syntaxeditor-extend-default-tagger

Solution

You can create a custom class that inherits the desired tagger. Then you can override the GetWordsToClassificationTypes() method and modify the Dictionary return from the base class. This method will be called multiple times, so you will need to add a check if the given key already exists. For this tutorial, we are going to extend the CSharpTagger and we will add "Telerik" keyword which will be marked by the tagger.

C#

public class MyTagger : CSharpTagger
{        
        public MyTagger(RadSyntaxEditorElement editor)
        : base(editor)
    {
    }

    protected override Dictionary<string, ClassificationType> GetWordsToClassificationTypes()
    {
        Dictionary<string, ClassificationType> baseTypes = base.GetWordsToClassificationTypes();
        if(!baseTypes.ContainsKey("Telerik"))
        {
            baseTypes.Add("Telerik", ClassificationTypes.Keyword); // or any other ClassificationTypes
        }
        return baseTypes;
    }
}

What's left is to register the extended predefined tagger.

C#

MyTagger myCSharpTagger = new MyTagger(this.editor.SyntaxEditorElement);
editor.TaggersRegistry.RegisterTagger(myCSharpTagger);

In this article
EnvironmentDescriptionSolution
Not finding the help you need?
Contact Support