Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
940 views
in Technique[技术] by (71.8m points)

c# - how to add a character at the beginning of each line in Richtextbox

I am doing an app that adds a certain character for each selected line if you click the button. for example "//" in each line in a Richtextbox and colored the text into red, it is like the function of comment out in visual studio

i tried this but it did'nt work

private void toolStripButton1_Click(object sender, EventArgs e)
    {
        int firstCharPosition = richTextBox1.GetFirstCharIndexOfCurrentLine();
        int lineNumber = richTextBox1.GetLineFromCharIndex(firstCharPosition);
        int lastCharPosition = richTextBox1.GetFirstCharIndexFromLine(lineNumber + 1);

        if (richTextBox1.SelectionLength > 0)
        {
            richTextBox1.SelectionColor = Color.Red;
            richTextBox1.SelectedText = "//" + richTextBox1.SelectedText.ToString();
        }
        else
        {
            richTextBox1.Select(firstCharPosition, lastCharPosition - firstCharPosition);
            richTextBox1.SelectionColor = Color.Red;
            richTextBox1.SelectedText = "//" + richTextBox1.SelectedText.ToString();
        }
    }

guys please help me thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
if (richTextBox1.Text.Length > 0 && richTextBox1.SelectionLength >= 0)
    {
        string[] lines = richTextBox1.Text.Split(new string[] { Environment.NewLine, "
", "
", "
" }, 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 ? "" : "
");
        }

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...