现已经实现将某一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
}文本搜索
我想的是用正则表达式实现:
Regex expression_search = new Regex(textBox2.Text); //不知道括号里能否这样写?
Match m_Re = expression_search.Match(textBox1中的内容,逐行读取); //这里将textBox1逐行读取,求教如何实现textbox中的内容逐行读取?
if (m_Re.Success)
{
//匹配成功,将该符合的文字标红,但不知如何实现?
// XXXXXX
}文本搜索
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”点按钮。
谢谢老师,已实现。但如果我想进一步实现这样的效果:搜索之后,符合匹配的所有文字都被标记出来选中,而不是一个一个依次出现,这样可以实现吗?
因为文本框不支持多重的高亮,需要重绘了。思路参考:http://bbs.csdn.net/topics/390588871