新手求教,怎么实现类似于Word中的搜索功能
新手求教,如何实现类似于Word中的搜索功能?
现已经实现将某一txt文件内容读入并显示到TextBox1中,现在要实现在另外一个textBox2中输入文字,然后搜索出textBox1中对应的文字并标红显示。
我想的是用正则表达式实现:
------解决方案--------------------
在界面上放两个文本框,一个按钮。
在第一个文本框中输入“123”
在第二个文本框中输入“123 1234 123 12 1234 234 123”
点按钮。
现已经实现将某一txt文件内容读入并显示到TextBox1中,现在要实现在另外一个textBox2中输入文字,然后搜索出textBox1中对应的文字并标红显示。
我想的是用正则表达式实现:
Regex expression_search = new Regex(textBox2.Text); //不知道括号里能否这样写?
Match m_Re = expression_search.Match(textBox1中的内容,逐行读取); //这里将textBox1逐行读取,求教如何实现textbox中的内容逐行读取?
if (m_Re.Success)
{
//匹配成功,将该符合的文字标红,但不知如何实现?
// XXXXXX
}
文本
搜索
------解决方案--------------------
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int n = textBox2.Text.IndexOf(textBox1.Text, textBox2.SelectionStart + textBox2.SelectionLength);
if (n == -1)
n = textBox2.Text.IndexOf(textBox1.Text, 0);
if (n >= 0)
{
textBox2.SelectionStart = n;
textBox2.SelectionLength = textBox1.Text.Length;
ActiveControl = textBox2;
}
}
}
}
在界面上放两个文本框,一个按钮。
在第一个文本框中输入“123”
在第二个文本框中输入“123 1234 123 12 1234 234 123”
点按钮。