popoy
popoy

Reputation: 33

How to remove a character at the beginning of each line in Richtextbox

I am creating an app that will remove a certain character for each selected line if you click the button.

For example: I have "//" in each line in a RichTextbox and coloured the text into red, it is like the function of comment & uncomment in Visual Studio.

The question is how can I remove "//" in each and every line and return the color in its default color?

This code is for adding "//" and coloring it red:

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();
}
}

Please help me how to uncomment the commented line with the new button.

Upvotes: 0

Views: 476

Answers (1)

Abdul Saleem
Abdul Saleem

Reputation: 10612

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);
}

Upvotes: 1

Related Questions