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

后端 未结 3 1705
花落未央
花落未央 2020-12-12 02:48

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 re

相关标签:
3条回答
  • 2020-12-12 03:22

    This might do the job

    if (richTextBox1.SelectionLength >= 0 && richTextBox1.Text.Length > 0)
    {
        int firstCharIndex = richTextBox1.GetFirstCharIndexOfCurrentLine();
        if (richTextBox1.SelectionLength == 0)
        {
            int lineNumber = richTextBox1.GetLineFromCharIndex(firstCharIndex);
            int lastCharIndex = richTextBox1.GetFirstCharIndexFromLine(lineNumber + 1);
            if (lastCharIndex == -1) lastCharIndex = richTextBox1.Text.Length;
            richTextBox1.Select(firstCharIndex, lastCharIndex - firstCharIndex);
            richTextBox1.SelectionColor = Color.Red;
            richTextBox1.SelectedText = "//" + richTextBox1.SelectedText;
            richTextBox1.Select(firstCharIndex--, lastCharIndex - firstCharIndex);
            richTextBox1.Focus();
        }
        else
        {
            int selStart = richTextBox1.SelectionStart;
            int selLen = richTextBox1.SelectionLength;
            richTextBox1.SelectionStart = selStart + selLen;
            int lastLineFirstChar = richTextBox1.GetFirstCharIndexOfCurrentLine();
            int lastLineNumber = richTextBox1.GetLineFromCharIndex(lastLineFirstChar);
            int lastLineLastChar = richTextBox1.GetFirstCharIndexFromLine(lastLineNumber + 1);
            if (lastLineLastChar == -1) lastLineLastChar = richTextBox1.Text.Length;
    
            string beforeSelection = richTextBox1.Text.Substring(0, firstCharIndex);
            string afterSelection = richTextBox1.Text.Substring(lastLineLastChar, richTextBox1.Text.Length - lastLineLastChar);
            string selectionText = richTextBox1.Text.Substring(firstCharIndex, lastLineLastChar - firstCharIndex);
    
            string[] lines = selectionText.Split(new string[] { Environment.NewLine, "\r", "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            string commentedText = "";
            for (int i = 0; i < lines.Length; i++) commentedText += "//" + lines[i] + "\r\n";
    
            richTextBox1.Text = beforeSelection + afterSelection;
            richTextBox1.SelectionStart = firstCharIndex;
            richTextBox1.SelectedText = commentedText;
            richTextBox1.Select(firstCharIndex, lastLineLastChar - firstCharIndex + (lines.Length * 2));
            richTextBox1.SelectionColor = Color.Red;
            richTextBox1.Focus();
        }
    }
    
    0 讨论(0)
  • 2020-12-12 03:41

    Manipulating a RichTextBox correctly is a little more involved than changing the Text. Here is a code example that will help you.

    Note that is never changes the Text directly, so it won't mess up the previous formatting and that it tries to restore the selection.

    It allows you to comment out several sections independently. It takes the comment string from a TextBox for testing and it restores to Black..

    enter image description here

    // get all line numbers that belong to the selection
    List<int> getSelectedLines(RichTextBox RTB)
    {
        List<int> lines = new List<int>();
    
        int sStart = RTB.SelectionStart;
        int sEnd = RTB.SelectionLength + sStart;
    
        int line1 = RTB.GetLineFromCharIndex(sStart);
        int line2 = RTB.GetLineFromCharIndex(sEnd);
    
        for (int l = line1; l <= line2; l++) lines.Add(l);
        return lines;
    
    }
    
    // prefix a line with a string
    void prependLine(RichTextBox RTB, int line, string s)
    {
        int sStart = RTB.SelectionStart;
        int sLength = RTB.SelectionLength;
    
        RTB.SelectionStart = RTB.GetFirstCharIndexFromLine(line);
        RTB.SelectionLength = 0;
        RTB.SelectedText = s;
    
        RTB.SelectionStart = sStart;
        RTB.SelectionLength = sLength;
    }
    
    // color one whole line
    void colorLine(RichTextBox RTB, int line, Color c)
    {
        int sStart = RTB.SelectionStart;
        int sLength = RTB.SelectionLength;
    
        RTB.SelectionStart = RTB.GetFirstCharIndexFromLine(line);
        RTB.SelectionLength = RTB.Lines[line].Length; ;
        RTB.SelectionColor = c;
    
        RTB.SelectionStart = sStart;
        RTB.SelectionLength = sLength;
    
    }
    
    // additional function, may come handy..
    void trimLeftLine(RichTextBox RTB, int line, int length)
    {
        int sStart = RTB.SelectionStart;
        int sLength = RTB.SelectionLength;
    
        RTB.SelectionStart = RTB.GetFirstCharIndexFromLine(line);
        RTB.SelectionLength = length;
        RTB.Cut();
    
        RTB.SelectionStart = sStart;
        RTB.SelectionLength = 0;
    }
    
    // remove a string token from the start of a line
    void trimLeftLine(RichTextBox RTB, int line, string token)
    {
        int sStart = RTB.SelectionStart;
        int sLength = RTB.SelectionLength;
    
        RTB.SelectionStart = RTB.GetFirstCharIndexFromLine(line);
        RTB.SelectionLength = token.Length;
        if (RTB.SelectedText == token) RTB.Cut();
    
        RTB.SelectionStart = sStart;
        RTB.SelectionLength = 0;
    }
    

    This is the comment button:

    private void button1_Click(object sender, EventArgs e)
    {
        List<int> lines = getSelectedLines(richTextBox1);
    
        foreach (int l in lines) prependLine(richTextBox1, l, tb_comment.Text);
        foreach (int l in lines) colorLine(richTextBox1, l, Color.Firebrick);
    
    }
    

    This is the uncomment button:

    private void button2_Click(object sender, EventArgs e)
    {
        List<int> lines = getSelectedLines(richTextBox1);
    
        foreach (int l in lines) trimLeftLine(richTextBox1, l, tb_comment.Text);
        foreach (int l in lines) colorLine(richTextBox1, l, Color.Black);
    
    }
    
    0 讨论(0)
  • 2020-12-12 03:46
    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();
        }
    
    0 讨论(0)
提交回复
热议问题