如何在RichEditControl中标记关键字

时间:2014-03-22 11:21:36

标签: devexpress keyword richedit-control

我在WindowsForm中使用DevExpress RichEditControl。

我试图让用户输入sql语句。

如何将关键字的颜色更改为BLUE,例如SELECT,FROM,ORDER BY用户输入?

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

您可以two方式实现它。

1.通过注册ISyntaxHighlightService,为T-SQL语言实现简化的语法突出显示。

2.您可以使用DevExpress.CodeParser库。它可用于解析以下语言的文本:C#,Visual Basic,JavaScript,HTML,XAML,CSS

此代码适用于first。您可以突出显示所需的关键字。 代码示例可在此处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;
    }
}

如果您想使用DevExpress.CodeParser library

实施

这是代码示例https://www.devexpress.com/Support/Center/Example/Details/E2993