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



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

例如:我在RichTextbox中的每一行都有"//",并将文本涂成红色,这就像comment&在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", "rn" }, 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 ? "" : "rn");
    }
    int selectStarIndx = richTextBox1.GetFirstCharIndexFromLine(startLine), selectEndIndx = richTextBox1.GetFirstCharIndexFromLine(endLine + 1);
    if (selectEndIndx == -1) selectEndIndx = richTextBox1.Text.Length;
    richTextBox1.Select(selectStarIndx, selectEndIndx - selectStarIndx);
    richTextBox1.Focus();
}
}

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

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", "rn" }, 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 ? "" : "rn");
        }
        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);
}

相关内容

最新更新