gpt4 book ai didi

c# - 如何突出显示具有不同于 RichTextBox 文本中所有其他选择的颜色的单词或短语?

转载 作者:行者123 更新时间:2023-12-05 03:52:52 33 4
gpt4 key购买 nike

主要目标是让用户在所有其他选定的单词或短语中将一个选项识别为当前选项。

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
if (results.Count > 0)
{
richTextBox1.SelectionStart = results[(int)numericUpDown1.Value - 1];
richTextBox1.SelectionLength = textBox1.Text.Length;
richTextBox1.SelectionColor = Color.Red;
richTextBox1.ScrollToCaret();
}
}

在这种情况下,在截图中,有3个搜索词的结果:System。所有结果都显示为黄色。

如果我将 NumericUpDown 值更改为 2,它会将结果中的第二个 System 单词着色为红色,然后当将 NumericUpDown 值更改为 3 时,它将最后一个系统结果着色为红色也。

我的问题是 2 和 3 都将显示为红色:我只希望当前选择的单词显示为红色,所有其他匹配应使用默认颜色。

In yellow

2 is in red

2 and 3 in red but only 3 should be in red 2 should be back to be in yellow

最佳答案

一些有助于构建可处理文本选择的类对象的建议。

此类应包含大部分(或全部)在给定文本中搜索关键字所需的逻辑,维护找到的匹配列表、它们的位置和长度,并允许对该列表进行基本导航匹配项,以及其他可以轻松扩展功能的配置。

在这里,TextSearcher1 类构造函数需要一个 RichTextBox 控件作为参数之一,加上一个用于正常选择的颜色和一个用于突出显示当前选择的颜色当匹配列表被导航时匹配。

▶ 搜索功能由 Regex.Matches() 处理方法,返回 MatchCollectionMatch对象,非常方便,因为每个 Match 都包含匹配项在文本中的位置(Index 属性)及其长度(Length 属性)。

▶ 导航功能由 BindingSource 提供目的。它的 DataSource 属性设置为 Regex.Matches() 返回的 MatchCollection 并且每次搜索一组新的关键字时它都会重置。< br/>此类在初始化时提供 MoveNext()MovePrevious() MoveLast()MoveFirst() 方法,加上一个 Position 属性,它标识集合中当前对象的索引 - 此处为 Match 对象。

CaseSensite 属性用于执行区分大小写或不区分大小写的搜索。添加是为了表明可以很简单地使用基于现有功能构建的更多功能来扩展基本功能(例如,文化敏感/不敏感搜索)。

要初始化 TextSearcher 类,将 RichTextBox 控件的实例和两个 Color 值传递给它的构造函数:

TextSearcher matchFinder = null;

public SomeForm()
{
InitializeComponent();
matchFinder = new TextSearcher(richTextBox1, Color.Yellow, Color.Red);
}

在示例中,您可以看到四个控件:

  • 上一个 (btnPrevious) 和下一个 (btnNext) 按钮,用于导航匹配列表。
  • 一个 NumericUpDown (nudGotoMatch),用于跳转到特定的匹配键。
  • 一个文本框(txtSearch),用于输入一个或多个关键字,由竖线分隔(竖线字符用于指定 Regex 中的备用匹配项,它可以替换为其他内容界面)。

由于 TextSearcher 类包含所有搜索和导航逻辑,因此在前端激活这些控件功能所需的代码很少,只需满足标准 UI 要求(例如设置 e.SuppressKeyPress = true 当在 TextBox 控件中按下 Enter 键时)。

private void txtSearch_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter) {
string keywords = (sender as Control).Text;
e.SuppressKeyPress = true;

if (!matchFinder.CurrentKeywords.Equals(keyword)) {
nudGotoMatch.Maximum = matchFinder.Search(keywords);
}
}
}

private void nudGotoMatch_ValueChanged(object sender, EventArgs e)
{
if (matchFinder.Matches.Count > 0) {
matchFinder.GotoMatch((int)nudGotoMatch.Value - 1);
}
}

private void btnPrevious_Click(object sender, EventArgs e)
{
matchFinder.PreviousMatch();
}

private void btnNext_Click(object sender, EventArgs e)
{
matchFinder.NextMatch();
}

它是这样工作的:

RichTextBox Search Utility

TextSearcher 类:

using System.Collections.Generic;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;

private class TextSearcher
{
private BindingSource m_bsMatches = null;
private RichTextBox m_Rtb = null;

public TextSearcher(RichTextBox rtb) : this(rtb, Color.Yellow, Color.Red) { }
public TextSearcher(RichTextBox rtb, Color selectionColor, Color currentColor)
{
m_Rtb = rtb;
SelectionColor = selectionColor;
CurrentColor = currentColor;
}

public string CurrentKeywords { get; private set; } = string.Empty;
public bool CaseSensitive { get; set; } = true;
public int CurrentIndex => m_bsMatches.Position;
public Match CurrentMatch => (Match)m_bsMatches.Current;
public MatchCollection Matches { get; private set; }
public Color SelectionColor { get; set; }
public Color CurrentColor { get; set; }

public void GotoMatch(int position)
{
SelectText(false);
m_bsMatches.Position = Math.Max(Math.Min(m_bsMatches.Count, position), 0);
SelectText(true);
}
public void NextMatch()
{
SelectText(false);
if (m_bsMatches != null && m_bsMatches.Position == m_bsMatches.Count - 1) {
m_bsMatches.MoveFirst();
}
else { m_bsMatches.MoveNext(); }
SelectText(true);
}

public void PreviousMatch()
{
SelectText(false);
if (m_bsMatches != null && m_bsMatches.Position > 0) {
m_bsMatches.MovePrevious();
}
else { m_bsMatches.MoveLast(); }
SelectText(true);
}

public int Search(string keywords)
{
if (CurrentKeywords.Equals(keywords)) return Matches.Count;
m_bsMatches?.Dispose();
CurrentKeywords = keywords;
var options = RegexOptions.Multiline |
(CaseSensitive ? RegexOptions.IgnoreCase : RegexOptions.None);
Matches = Regex.Matches(m_Rtb.Text, keywords, options);
if (Matches != null) {
m_Rtb.SelectAll();
m_Rtb.SelectionColor = m_Rtb.ForeColor;
m_Rtb.SelectionStart = 0;
m_bsMatches = new BindingSource(Matches, null);
SelectKeywords();
return Matches.Count;
}
return 0;
}

private void SelectKeywords()
{
foreach (Match m in Matches) {
SelectText(false);
NextMatch();
}
m_bsMatches.MoveFirst();
}
private void SelectText(bool current)
{
m_Rtb.Select(CurrentMatch.Index, CurrentMatch.Length);
m_Rtb.SelectionColor = current ? CurrentColor : SelectionColor;
}
}

1 - 我知道,好名字!我花了一段时间才想到它,所以请不要更改它:)

关于c# - 如何突出显示具有不同于 RichTextBox 文本中所有其他选择的颜色的单词或短语?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62009599/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com