Windows 窗体
一组用于开发图形用户界面的 .NET Framework 托管库。
113 个问题
我有一个 listView 控件名称 lvnf 中的项列表。
当我在 listView 中选择一个项目时,它正在读取硬盘上的项目文件,并在 richTextBox1 中显示其内容。
将文件内容添加到 richTextBox1 后,我想突出显示 richTextBox1 中的特定单词,但将此单词着色为黄色。
问题在于它正在对 richTextBox1 中的所有内容进行着色。
foreach 循环中的 myword 是每个单词(字符串) 我想在 richTextBox1 中用黄色着色,但它将 richTextBox1 中的所有内容都涂成黄色。
void lvnf_SelectedIndexChanged(object sender, EventArgs e)
{
if (listViewCostumControl1.lvnf.SelectedItems.Count > 0)
{
results = new List<int>();
richTextBox1.Text = File.ReadAllText(listViewCostumControl1.lvnf.Items[listViewCostumControl1.lvnf.SelectedIndices[0]].Text);
FileInfo fi = new FileInfo(listViewCostumControl1.lvnf.Items[listViewCostumControl1.lvnf.SelectedIndices[0]].Text);
lblfilesizeselected.Text = ExtensionMethods.ToFileSize(fi.Length);
lblfilesizeselected.Visible = true;
filePath = Path.GetDirectoryName(fi.FullName);
string word = textBoxSearchBox.Text;
string[] test = word.Split(new string[] { ",," }, StringSplitOptions.None);
foreach (string myword in test)
{
HighlightPhrase(richTextBox1, myword, Color.Yellow);
lblviewerselectedfile.Text = results.Count.ToString();
lblviewerselectedfile.Visible = true;
if (results.Count > 0)
{
numericUpDown1.Maximum = results.Count;
numericUpDown1.Enabled = true;
richTextBox1.SelectionStart = results[(int)numericUpDown1.Value];
}
}
}
}
此方法是 HighlightPhrase,它应该在 richTextBox1 中找到单词
void HighlightPhrase(RichTextBox box, string phrase, Color color)
{
int pos = box.SelectionStart;
string s = box.Text;
for (int ix = 0; ;)
{
int jx = s.IndexOf(phrase, ix, StringComparison.CurrentCultureIgnoreCase);
if (jx < 0)
{
break;
}
else
{
box.SelectionStart = jx;
box.SelectionLength = phrase.Length;
box.SelectionColor = color;
ix = jx + 1;
results.Add(jx);
}
}
box.SelectionStart = pos;
box.SelectionLength = 0;
}
Note:此问题总结整理于: Why when highlighting word/s in richTextBox it's highlighting all the text in the richTextBox ?
根据我的测试,我发现我可以运行您的代码来为 Richtextbox 中的特定单词着色。
另外,请注意,您在这里缺少一些代码,我对其进行了一些更改。
更改此内容
for (int ix = 0; ;)
到:
for (int ix = 0;ix<s.Length ;ix++)
这是一个适合我的代码示例。(仅更改了上述代码)
List<int> results = new List<int>();
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
richTextBox1.Text = File.ReadAllText(listView1.Items[listView1.SelectedIndices[0]].Text);
FileInfo fi = new FileInfo(listView1.Items[listView1.SelectedIndices[0]].Text);
//lblfilesizeselected.Text = ExtensionMethods.ToFileSize(fi.Length);
//lblfilesizeselected.Visible = true;
string filePath = Path.GetDirectoryName(fi.FullName);
string word = textBox1.Text;
string[] test = word.Split(new string[] { ",," }, StringSplitOptions.None);
foreach (string myword in test)
{
HighlightPhrase(richTextBox1, myword, Color.Yellow);
//lblviewerselectedfile.Text = results.Count.ToString();
//lblviewerselectedfile.Visible = true;
if (results.Count > 0)
{
numericUpDown1.Maximum = results.Count;
numericUpDown1.Enabled = true;
richTextBox1.SelectionStart = results[(int)numericUpDown1.Value];
}
}
}
}
void HighlightPhrase(RichTextBox box, string phrase, Color color)
{
int pos = box.SelectionStart;
string s = box.Text;
for (int ix = 0;ix<s.Length ;ix++)
{
int jx = s.IndexOf(phrase, ix, StringComparison.CurrentCultureIgnoreCase);
if (jx < 0)
{
break;
}
else
{
box.SelectionStart = jx;
box.SelectionLength = phrase.Length;
box.SelectionColor = color;
ix = jx + 1;
results.Add(jx);
}
}
box.SelectionStart = pos;
box.SelectionLength = 0;
}
private void Form1_Load(object sender, EventArgs e)
{
listView1.SelectedIndexChanged += listView1_SelectedIndexChanged;
listView1.View = View.Details;
listView1.Columns.Add("Path");
listView1.Columns.Add("Name");
ListViewItem lvi = new ListViewItem();
lvi.Text = "D:\\1.txt";
lvi.SubItems.Add("test1");
ListViewItem lvi1 = new ListViewItem();
lvi1.Text = "D:\\2.txt";
lvi1.SubItems.Add("test2");
ListViewItem lvi2 = new ListViewItem();
lvi2.Text = "D:\\3.txt";
lvi2.SubItems.Add("test3");
listView1.Items.Add(lvi);
listView1.Items.Add(lvi1);
listView1.Items.Add(lvi2);
}
结果:
如果回复有帮助,请点击“接受答案”并点赞。
注意:如果您想接收此线程的相关电子邮件通知,请按照我们文档中的步骤启用电子邮件通知。