c# RichTextBox 实现查找字符串

玄魂 2008-05-08 03:59:11
做简单解释,给个实际例子
...全文
724 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
qiang732520885 2011-11-12
  • 打赏
  • 举报
回复
就是啊,为什么上面的代码就是找不到内容啊,返回值总是-1
Choi57671452 2008-05-12
  • 打赏
  • 举报
回复

private void button1_Click(object sender, EventArgs e)
{
string find = this.textBox1.Text;
int index = this.richTextBox1.Find(find);
if (index < 0)
{
MessageBox.Show("查找不到要搜索的字符串");
return;
}
this.richTextBox1.SelectionStart = index;
this.richTextBox1.SelectionLength = find.Length;
this.richTextBox1.Focus();
}
玄魂 2008-05-12
  • 打赏
  • 举报
回复
就是载入的一段文档
比如
“你哦iasd飞阿斯顿杀毒发生的发生了对方杀毒 杀毒飞
安装”
我现在想找“安”字
王集鹄 2008-05-11
  • 打赏
  • 举报
回复
richTextBox内容是什么?你想搜什么?总之就是怎么让我们看到你说的现象?[img=http://p.blog.csdn.net/images/p_blog_csdn_net/zswang/%E5%9B%A7.gif]图[/img]
玄魂 2008-05-11
  • 打赏
  • 举报
回复
不管用什么方法,总是搜不到,不知道怎么回事
王集鹄 2008-05-10
  • 打赏
  • 举报
回复
RichTextBox有Find()方法:
private void button1_Click(object sender, EventArgs e)
{
if (richTextBox1.Find("as",
richTextBox1.SelectionStart + richTextBox1.SelectionLength,
richTextBox1.TextLength, RichTextBoxFinds.None) < 0) // 为-1表示没有找到
{
richTextBox1.SelectionStart = 0; // 光标设置为0位置
richTextBox1.SelectionLength = 0;
}
richTextBox1.Focus();
}

lsj_zrp 2008-05-10
  • 打赏
  • 举报
回复
private int FindPlace=0;
private void btnFindNext_click(object sender,System.EventArgs e)
{
if(txtToSearch.Text!="")
{
Form1 mainForm=(Form)this.Owner;
if(mainForm.RichTextBox.Text.Length>0)
{
if((FindPlace=mainForm.RichTextBox.Text.Indexof(txtToSearch.Text))==-1)
{
MessageBox.Show("没有搜索到!");
FindPlace=0;
}
else
{
mainForm.RichTextBox.Select(FindPlace,txtToSearch.Text.Length);
FindPlace+=txtToSearch.Text.Length;
mainForm.Activate();
}
}
}
}
其中Form1是一个包含RichTextBox的主窗口,txtToSearch是主窗口弹出的查询窗口输入查询的
类似于打开一个txt文档,按F5查询,Form1就是那个txt文档,txtToSearch就是"查找内容"后面的那个textbox
代码很简单的,看下就会了,手动输入的,可能有错误!

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧