1

I am using DevExpress RichEditControl in WindowsForm.

I am trying to let the user type sql statements.

How can I change the the color of the keyword to BLUE such as SELECT, FROM, ORDER BY hen the user types it?

Jassim Rahma
  • 93
  • 1
  • 3
  • 13

2 Answers2

0

Take a look at RichEditControl's Syntax Highlighting feature.

Related example: How to implement T-SQL language syntax highlighting by creating Syntax Highlight Tokens manually

DmitryG
  • 17,677
  • 1
  • 30
  • 53
0

You can implement it in two ways.

1.Implement simplified syntax highlighting for the T-SQL language by registering the ISyntaxHighlightService.

2.You can use the DevExpress.CodeParser library. It can be utilized to parse text in the following languages:C#,Visual Basic,JavaScript,HTML,XAML,CSS

This code is for the first one. you can highlight the keywords you want. Code Example is available here https://www.devexpress.com/Support/Center/Example/Details/E4139.

public partial class Form1 : Form
{
    public Form1() 
    {
        InitializeComponent();
        richEditControl1.ActiveViewType = DevExpress.XtraRichEdit.RichEditViewType.Draft;
        richEditControl1.ReplaceService<ISyntaxHighlightService>(new CustomSyntaxHighlightService(richEditControl1.Document));

    }
}

public class CustomSyntaxHighlightService : ISyntaxHighlightService 
{
    readonly Document document;
    SyntaxHighlightProperties defaultSettings = new SyntaxHighlightProperties()
    { 
       ForeColor = Color.Black 
    };
    SyntaxHighlightProperties keywordSettings = new SyntaxHighlightProperties() 
    { 
       ForeColor = Color.Blue
    };
    SyntaxHighlightProperties stringSettings = new SyntaxHighlightProperties()
    {
       ForeColor = Color.Green
    };

    string[] keywords = new string[] 
    { 
        "INSERT", "SELECT", "CREATE", "TABLE", "USE", "IDENTITY", "ON", "OFF", "NOT", "NULL", "WITH", "SET" "ORDER BY" 
    };

    public CustomSyntaxHighlightService(Document document)
    {
        this.document = document;
    }

    private List<SyntaxHighlightToken> ParseTokens() 
    {
        List<SyntaxHighlightToken> tokens = new List<SyntaxHighlightToken>();
        DocumentRange[] ranges = null;
        ranges = document.FindAll("'", SearchOptions.None);
        for (int i = 0; i < ranges.Length / 2; i++)
        {
            tokens.Add(new SyntaxHighlightToken(ranges[i * 2].Start.ToInt(), 
                ranges[i * 2 + 1].Start.ToInt() - ranges[i * 2].Start.ToInt() + 1, stringSettings));
        }
        for (int i = 0; i < keywords.Length; i++)
        {
            ranges = document.FindAll(keywords[i], SearchOptions.CaseSensitive | SearchOptions.WholeWord);

            for (int j = 0; j < ranges.Length; j++)
            {
                if (!IsRangeInTokens(ranges[j], tokens))
                    tokens.Add(new SyntaxHighlightToken(ranges[j].Start.ToInt(), ranges[j].Length, keywordSettings));
            }
        }
        tokens.Sort(new SyntaxHighlightTokenComparer());
        AddPlainTextTokens(tokens);
        return tokens;
    }

    private void AddPlainTextTokens(List<SyntaxHighlightToken> tokens)
    {
        int count = tokens.Count;
        if (count == 0) 
        {
            tokens.Add(new SyntaxHighlightToken(0, document.Range.End.ToInt(), defaultSettings));
            return;
        }
        tokens.Insert(0, new SyntaxHighlightToken(0, tokens[0].Start, defaultSettings));

        for (int i = 1; i < count; i++)
        {
           tokens.Insert(i * 2, new SyntaxHighlightToken(tokens[i * 2 - 1].End,tokens[i * 2].Start - tokens[i * 2 - 1].End, defaultSettings));
        }
           tokens.Add(new SyntaxHighlightToken(tokens[count * 2 - 1].End,document.Range.End.ToInt() - tokens[count * 2 - 1].End, defaultSettings));
    }

   private bool IsRangeInTokens(DocumentRange range, List<SyntaxHighlightToken> tokens)
   {
        for (int i = 0; i < tokens.Count; i++) 
        {
            if (range.Start.ToInt() >= tokens[i].Start && range.End.ToInt() <= tokens[i].End)
                return true;
        }
        return false;
    }
    public void ForceExecute()
    {
        Execute();
    }

    public void Execute()
    {
        document.ApplySyntaxHighlight(ParseTokens());
    }
}

public class SyntaxHighlightTokenComparer : IComparer<SyntaxHighlightToken>
{
    public int Compare(SyntaxHighlightToken x, SyntaxHighlightToken y) 
    {
        return x.Start - y.Start;
    }
}

If you want to implement using DevExpress.CodeParser library

here is the code sample https://www.devexpress.com/Support/Center/Example/Details/E2993