【问题标题】:How can I add to a RichTextbox also the colored string and the rest of the string?如何将彩色字符串和字符串的其余部分也添加到 RichTextbox?
【发布时间】:2017-01-31 14:33:00
【问题描述】:
foreach(string line in lines)
{
    richTextBox1.AppendText(line);
    RichTextBoxExtensions.AppendText(richTextBox1, "Ready: ", Color.Red);
}

如果行是

“你好世界”

所以我想要的是在RichTextBox1 的第一行:

准备好你好世界

其中 Ready 仅显示为红色 Ready。

在下一行再次

准备好你好

Again Ready 是红色的,但是你好,它的原始颜色没有改变。

但是我得到的是混乱的世界 Ready 被添加到最后一行之后,它不是仅在第一行中的红色。

还在RichTextBox 中将所有行和Ready 添加为文本块而不是行。

我想在运行程序时在RichTextBox 中看到的是行:

准备好了:世界你好
准备好了:你好你好
准备好了:这是一条线
准备好了:大家好

只有

准备好了:

红色

public class RichTextBoxExtensions
{
    public static void AppendText(RichTextBox box, string text, Color color)
    {
        box.SelectionStart = box.TextLength;
        box.SelectionLength = 0;

        box.SelectionColor = color;
        box.AppendText(text);
        box.SelectionColor = box.ForeColor;
    }
    public static void UpdateText(RichTextBox box, string find, string replace, Color? color)
    {
        box.SelectionStart = box.Find(find, RichTextBoxFinds.Reverse);
        box.SelectionLength = find.Length;
        box.SelectionColor = color ?? box.SelectionColor;
        box.SelectedText = replace;
    }
}

【问题讨论】:

    标签: c# .net winforms richtextbox


    【解决方案1】:

    将 for 循环中的 2 行代码颠倒过来,并添加 Environment.NewLine。 这个小修正:

    foreach (string line in lines)
    {
        RichTextBoxExtensions.AppendText(richTextBox1, "Ready: ", Color.Red);
        richTextBox1.AppendText(line + Environment.NewLine);
    }
    

    用这个列表测试它:

    List<string> lines = new List<string>() { "Hello world", "hi hello", "this is a line"};
    

    带来这个结果:

    【讨论】:

      【解决方案2】:

      也许你可以这样做

      using System;
      using System.Drawing;
      using System.Windows.Forms;
      
      namespace WindowsFormsApplication1
      {
      public partial class Form1 : Form
      {
          public Form1()
          {
              InitializeComponent();
          }
      
          private void Form1_Load(object sender, EventArgs e)
          {
              richTextBox1.Font = new Font("Consolas", 18f, FontStyle.Bold);
              richTextBox1.BackColor = Color.AliceBlue;
              string[] words =
              {
                  "a",
                  "b",
                  "c",
                  "d",
                  "e",
                  "f",
                  "g."
              };
              Color[] colors =
              {
                  Color.Aqua,
                  Color.CadetBlue,
                  Color.Cornsilk,
                  Color.Gold,
                  Color.HotPink,
                  Color.Lavender,
                  Color.Moccasin
              };
              for (int i = 0; i < words.Length; i++)
              {
                  string word = words[i];
                  Color color = colors[i];
                  {
                      richTextBox1.SelectionBackColor = color;
                      richTextBox1.AppendText(word);
                      richTextBox1.SelectionBackColor = Color.AliceBlue;
                      richTextBox1.AppendText(" ");
                  }
              }
          }
      }
      

      }

      【讨论】:

        猜你喜欢
        • 2013-02-02
        • 2022-06-20
        • 2017-05-19
        • 1970-01-01
        • 1970-01-01
        • 2010-12-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多