1

我创建了一个应用程序,它将注释或取消注释突出显示的行或当前行并将该行着色为绿色。我有 2 个按钮,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 中研究它,但我没有找到方法,谢谢,那里的支持没有回答我的问题

4

0 回答 0