RichEditControl中的注释/取消注释功能

时间:2015-03-11 11:08:53

标签: c# winforms devexpress richedit-control

我已经创建了一个应用程序,可以评论或取消注释突出显示的行或当前行,并为该行添加绿色。我有2个按钮1用于评论,1个用于取消注释。这是我的代码

 DocumentRange lineRange = null;
    bool isSelectionLocked = false;

    void SetLineRangeFormatting(DocumentRange currentLineRange, System.Drawing.Color rangeColor)
    {
        CharacterProperties cp = richEditControl1.Document.BeginUpdateCharacters(currentLineRange);
        cp.ForeColor = rangeColor;
        richEditControl1.Document.EndUpdateCharacters(cp);
    }

    DocumentRange GetNewLineRange(DocumentPosition caret)
    {
        isSelectionLocked = true;
        DocumentPosition currentPosition = richEditControl1.Document.CaretPosition;

        StartOfLineCommand startOfLineCommand = new StartOfLineCommand(richEditControl1);
        EndOfLineCommand endOfLineCommand = new EndOfLineCommand(richEditControl1);

        startOfLineCommand.Execute();

        int start = richEditControl1.Document.CaretPosition.ToInt();

        endOfLineCommand.Execute();

        int length = richEditControl1.Document.CaretPosition.ToInt() - start;

        DocumentRange range = richEditControl1.Document.CreateRange(start, length);
        DocumentRange range2 = richEditControl1.Document.CreateRange(start, length + 1);

        string text = richEditControl1.Document.GetText(range2);

        richEditControl1.Document.CaretPosition = currentPosition;
        isSelectionLocked = false;

        if (text.EndsWith(Environment.NewLine))
        {
            return range2;
        }
        else
        {
            return range;
        }
    }

    private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
    {
        if (isSelectionLocked) return;
        if (!lineRange.Contains(richEditControl1.Document.CaretPosition))
        {
            //SetLineRangeFormatting(lineRange, System.Drawing.Color.Transparent);
            lineRange = GetNewLineRange(richEditControl1.Document.CaretPosition);
            SetLineRangeFormatting(lineRange, System.Drawing.Color.Green);
        } 
    }

    private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
    {

    }

但它没有正常工作请帮助我是新手,我在devexpress研究它但我没有找到办法做到这一点谢谢,那里的支持没有回答我的问题

0 个答案:

没有答案