【问题标题】:how to add a character at the beginning of each line in Richtextbox如何在Richtextbox的每一行的开头添加一个字符
【发布时间】:2019-08-10 06:23:21
【问题描述】:

我正在做一个应用程序,如果你点击按钮,它会为每个选定的行添加一个特定的字符。例如在Richtextbox中的每一行中添加“//”并将文本着色为红色,这就像Visual Studio中的注释功能

我试过了,但是没用

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

请大家帮帮我,谢谢!

【问题讨论】:

    标签: c# winforms richtextbox


    【解决方案1】:
    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();
        }
    

    【讨论】:

    • 从 StringSplitOptions.RemoveEmptyEntries 更改为 StringSplitOptions.None
    • 先生,它现在工作正常,我如何取消注释另一个按钮中的注释行.. 先生,请帮助我,谢谢
    • 请作为一个新问题提出。我来回答
    【解决方案2】:

    正确操作RichTextBox比更改Text要复杂一些。这是一个可以帮助您的代码示例。

    请注意,它永远不会直接更改 Text,因此它不会弄乱以前的格式,并且它会尝试恢复选择。

    它允许您独立注释掉几个部分。它从TextBox 获取注释字符串进行测试,然后恢复为Black..

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

    这是评论按钮:

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

    这是取消注释按钮:

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

    【讨论】:

      【解决方案3】:

      这可能会做的工作

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

      【讨论】:

      • 不客气。但是当我测试时,有一个问题。不介意就跳过。否则我还有一个解决方案。为此,您必须在richTextBox 中写入10 或12 行。一起选择第 3、4、5 行。通过点击事件评论它。它变成红色。之后选择第 6、7,8 行并对其进行评论。它变成红色,但上面的行返回黑色。如果您需要解决方案,请告诉我
      • 啊,我现在看到问题了.. 先生,如何解决这个问题?
      • 如果我有一个新按钮,如何取消注释注释文本?谢谢你回答我先生
      • 先生,您好,先生,我发现了一个问题.. 我已经写了 10 行,我一起选择 1 和 2 并评论它,然后它变成红色,然后我选择 2 和 4,它变成红色所有 10 行
      • 我去看看,很快就会发帖
      猜你喜欢
      • 1970-01-01
      • 2011-01-07
      • 1970-01-01
      • 2015-05-07
      • 1970-01-01
      • 2017-01-04
      • 1970-01-01
      • 2013-08-06
      相关资源
      最近更新 更多