[英]How can I search in a richTextBox text for a text not a single word/string?
[英]How can I add to a RichTextbox also the colored string and the rest of the string?
foreach(string line in lines)
{
richTextBox1.AppendText(line);
RichTextBoxExtensions.AppendText(richTextBox1, "Ready: ", Color.Red);
}
如果線是
“你好,世界”
所以我想擁有的是RichTextBox1
的第一行:
准備好世界
其中“就緒”為紅色僅“就緒”。
再次在下一行
准備好你好
再次就緒是紅色,但是您好,它的原始顏色沒有改變。
但是我得到的是混亂的世界,在下一行的末尾添加了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;
}
}
也許你可以這樣
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(" ");
}
}
}
}
}
在for循環中反轉兩行代碼,並添加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"};
導致以下結果:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.