如何在richditcontrol中标记关键字



我在WindowsForm中使用DevExpress RichEditControl

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

如何更改关键字的颜色为蓝色,如SELECT, FROM, ORDER BY,然后用户键入它?

看看RichEditControl的语法高亮功能。

相关示例:如何通过手动创建语法突出显示令牌来实现T-SQL语言语法突出显示

可以用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

相关内容

  • 没有找到相关文章

最新更新