1

我在 WindowsForm 中使用 DevExpress RichEditControl。

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

当用户键入关键字时,如何将关键字的颜色更改为蓝色,例如 SELECT、FROM、ORDER BY?

4

2 回答 2

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

于 2014-03-23T04:27:09.230 回答
0

看看 RichEditControl 的语法高亮功能。

相关示例:如何通过手动创建语法高亮标记来实现 T-SQL 语言语法高亮

于 2014-03-22T16:25:53.867 回答