如何删除Richtextbox中每行开头的字符

时间:2015-03-06 09:30:30

标签: c# winforms richtextbox

我正在创建一个应用程序,如果单击按钮,将删除每个选定行的特定字符。

例如:我在RichTextbox的每一行都有“//”并将文字涂成红色,这就像评论&的功能一样。在Visual Studio中取消注释。

问题是我如何在每一行中删除“//”并以默认颜色返回颜色?

此代码用于添加“//”并将其着色为红色:

private void toolStripButton1_Click(object sender, EventArgs e)
{
if (richTextBox1.Text.Length > 0 && richTextBox1.SelectionLength >= 0)
{
    string[] lines = richTextBox1.Text.Split(new string[] { Environment.NewLine, "\n", "\r", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
    Color normalColor = Color.Black, commentColor = Color.Red;
    int selStart = richTextBox1.SelectionStart, selEnd = selStart + richTextBox1.SelectionLength,
    startLine = -1, endLine = -1, lineSum = 0, k = 0;

    for (k = 0; k < lines.Length; k++)
        if (startLine == -1)
        {
            if ((lineSum += lines[k].Length + 1) > selStart)
            {
                startLine = k;
                if (selEnd <= lineSum) endLine = k;
            }
        }
        else if (endLine == -1)
        {
            if ((lineSum += lines[k].Length + 1) >= selEnd)
                endLine = k;
        }
        else break;

    for (int i = 0; i < lines.Length; i++)
        lines[i] = (i >= startLine && i <= endLine ? "//" : "") + lines[i];

    richTextBox1.Text = "";
    richTextBox1.SelectionStart = 0;
    for (int i = 0; i < lines.Length; i++)
    {
        richTextBox1.SelectionStart = richTextBox1.Text.Length;
        richTextBox1.SelectionColor = lines[i].TrimStart().StartsWith("//") ? commentColor : normalColor;
        richTextBox1.SelectedText = lines[i] += (i == lines.Length - 1 ? "" : "\r\n");
    }

    int selectStarIndx = richTextBox1.GetFirstCharIndexFromLine(startLine), selectEndIndx = richTextBox1.GetFirstCharIndexFromLine(endLine + 1);
    if (selectEndIndx == -1) selectEndIndx = richTextBox1.Text.Length;

    richTextBox1.Select(selectStarIndx, selectEndIndx - selectStarIndx);
    richTextBox1.Focus();
}
}

请帮我解释如何使用新按钮取消注释注释行。

1 个答案:

答案 0 :(得分:1)

private void commentOrUnComment(RichTextBox rtb, bool isUnComment)
{
    if (rtb.Text.Length > 0 && rtb.SelectionLength >= 0)
    {
        string[] lines = rtb.Text.Split(new string[] { Environment.NewLine, "\n", "\r", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
        Color normalColor = Color.Black, commentColor = Color.Red;
        int selStart = rtb.SelectionStart, selEnd = selStart + rtb.SelectionLength,
        startLine = -1, endLine = -1, lineSum = 0, k = 0;

        for (k = 0; k < lines.Length; k++)
            if (startLine == -1)
            {
                if ((lineSum += lines[k].Length + 1) > selStart)
                {
                    startLine = k;
                    if (selEnd <= lineSum) endLine = k;
                }
            }
            else if (endLine == -1)
            {
                if ((lineSum += lines[k].Length + 1) >= selEnd)
                    endLine = k;
            }
            else break;

        for (int i = 0; i < lines.Length; i++)
            if (isUnComment)
                lines[i] = (i >= startLine && i <= endLine ? (lines[i].TrimStart().StartsWith("//") ? lines[i].Substring(0, lines[i].IndexOf("//"))
                        + lines[i].Substring(lines[i].IndexOf("//") + 2) : lines[i]) : lines[i]);
            else
                lines[i] = (i >= startLine && i <= endLine ? "//" : "") + lines[i];

        rtb.Text = "";
        rtb.SelectionStart = 0;
        for (int i = 0; i < lines.Length; i++)
        {
            rtb.SelectionStart = rtb.Text.Length;
            rtb.SelectionColor = lines[i].TrimStart().StartsWith("//") ? commentColor : normalColor;
            rtb.SelectedText = lines[i] += (i == lines.Length - 1 ? "" : "\r\n");
        }

        int selectStarIndx = rtb.GetFirstCharIndexFromLine(startLine), selectEndIndx = rtb.GetFirstCharIndexFromLine(endLine + 1);
        if (selectEndIndx == -1) selectEndIndx = rtb.Text.Length;

        rtb.Select(selectStarIndx, selectEndIndx - selectStarIndx);
        rtb.Focus();
    }
}

private void btnComment_Click(object sender, EventArgs e)
{
    commentOrUnComment(richTextBox1, false);
}

private void btnUncomment_Click(object sender, EventArgs e)
{
    commentOrUnComment(richTextBox1, true);
}