C# 如何编写查找函数
我有一个使用查找函数的 C# 程序,但是它能够找到该单词,但不会突出显示 richTextBox 中找到的单词。
有人可以建议我有关代码吗?
谢谢。
查找函数类形式:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Syscrawl
{
public partial class Find_Form : Form
{
FTK_Menu_Browsing_History fmbh = new FTK_Menu_Browsing_History();
public Find_Form()
{
InitializeComponent();
}
public void searchButton_Click(object sender, EventArgs e)
{
string s1 = fmbh.getSearchBrowsing().ToLower();
string s2 = textBoxSearch.Text.ToLower();
if (s1.Contains(s2))
{
MessageBox.Show("Word found!");
this.fmbh.richTextBoxBrowsing.Find(s2);
this.fmbh.richTextBoxBrowsing.SelectionLength = s2.Length;
this.fmbh.richTextBoxBrowsing.SelectionColor = Color.Red;
this.Close();
}
else
{
MessageBox.Show("Word not found!");
}
}
}
}
I have a C# program that utilizes a find function however it is able to find the word but does not highlights the found word in the richTextBox.
Can someone please advise me on the codes?
Thanks.
Find Function Class Form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Syscrawl
{
public partial class Find_Form : Form
{
FTK_Menu_Browsing_History fmbh = new FTK_Menu_Browsing_History();
public Find_Form()
{
InitializeComponent();
}
public void searchButton_Click(object sender, EventArgs e)
{
string s1 = fmbh.getSearchBrowsing().ToLower();
string s2 = textBoxSearch.Text.ToLower();
if (s1.Contains(s2))
{
MessageBox.Show("Word found!");
this.fmbh.richTextBoxBrowsing.Find(s2);
this.fmbh.richTextBoxBrowsing.SelectionLength = s2.Length;
this.fmbh.richTextBoxBrowsing.SelectionColor = Color.Red;
this.Close();
}
else
{
MessageBox.Show("Word not found!");
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要首先选择您要查找的内容。这:
之后你就可以进行整个突出显示。另一个提示,为了防止选择过程中闪烁,请在表单中使用以下代码:
在选择任何内容之前将 _doPaint 设置为 false,在选择之后将其设置为 true。
希望我能帮忙!
You need to select what you are looking for first. This:
After that you can make the whole highlightining. Another tip, to prevent the flickering in the selection process, use this code in your form:
Before selecting anything set _doPaint to false and after the selection set it to true.
Hope I can help!
您需要调用 s1.IndexOf(s2, StringComparison.CurrentCultureIgnoreCase) 来查找匹配的位置。
此外,看起来您的“查找”表单创建了自己的“历史记录”表单实例;它不使用现有实例。
您应该考虑接受构造函数参数。
You need to call
s1.IndexOf(s2, StringComparison.CurrentCultureIgnoreCase)
to find the position of the match.Also, it looks like your Find form creates its own instance of the History form; it doesn't use the existing instance.
You should consider accepting a constructor parameter.