【问题标题】:How to change background color of the certain line in the RichTextBox?如何更改 RichTextBox 中某行的背景颜色?
【发布时间】:2011-03-01 13:29:36
【问题描述】:

我想改变整行的颜色,不管文字有没有。这是一些解释图片:

.

我找到了一些解决方案here,但我希望有一个更简单的解决方案。

【问题讨论】:

  • +1 为伟大的插图:]

标签: .net winforms c#-4.0


【解决方案1】:

不,你首先要选择线,然后你必须设置颜色:

 public void MarkSingleLine()
 {
     int firstCharOfLineIndex = myRichTextBox.GetFirstCharIndexOfCurrentLine();
     int currentLine = richTextBox1.GetLineFromCharIndex(firstCharOfLineIndex);
     this.myRichTextBox.Select(firstCharOfLineIndex, currentLine);
     this.myRichTextBox.SelectionBackColor = Color.Aqua;
     this.myRichTextBox.Select(0, 0);
 }

【讨论】:

    【解决方案2】:

    好的,那么也许是这个 (found here):

    private void richTextBox1_MouseClick(object sender, MouseEventArgs e, Color color)
    {
        int firstcharindex = richTextBox1.GetFirstCharIndexOfCurrentLine();
        int currentline = richTextBox1.GetLineFromCharIndex(firstcharindex);
        string currentlinetext = richTextBox1.Lines[currentline];
        richTextBox1.SelectionBackColor = color;
        richTextBox1.Select(firstcharindex, currentlinetext.Length);
    }
    

    这个 sn-p 应该可以解决你的问题 ;-)

    【讨论】:

      【解决方案3】:

      你可以使用这段代码:

      private void richTextBox_LOG_write_text(string text, Color text_color, Color background_color)
          {
              try
              {
                  if(richTextBox_LOG.InvokeRequired == true)
                  {
                      Invoke(new Delegate_void_string_colortext_colorbackground(richTextBox_LOG_write_text), new object[] { text, text_color, background_color  });
                  }
                  int text_size = richTextBox_LOG.Text.Length;
                  richTextBox_LOG.AppendText(text);
                  richTextBox_LOG.Select(text_size, text.Length);
                  if(text_color == null)
                  {
                      text_color = Color.Black;
                  }
                  richTextBox_LOG.SelectionColor = text_color;
                  if(background_color != null)
                  {
                      richTextBox_LOG.SelectionBackColor = background_color;
                  }
              }
              catch { }
          }
      

      【讨论】:

        猜你喜欢
        • 2015-03-26
        • 2014-03-13
        • 2021-12-30
        • 2014-09-17
        • 1970-01-01
        • 1970-01-01
        • 2020-02-11
        • 1970-01-01
        • 2011-11-11
        相关资源
        最近更新 更多