
C# 窗体编程 Windows编程 richTextBox的Find方法怎么用??
1个回答
展开全部
给你一段代码,我以前写的,可以往前跟往后查找
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MyTxt
{
public partial class find : Form
{
public find()
{
InitializeComponent();
}
Form1 f1;
int index = 0;
private void button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked)
{
if (textBox1.Text.Length <= 0)
return;
if (index == 0)//如果光标在最开始
{
MessageBox.Show("未找到");
return;
}
//搜索的起点
int startIndex = index - textBox1.Text.Length;
if (startIndex < 0)
startIndex = 0;
//搜索
index = f1.richTextBox1.Text.IndexOf(textBox1.Text, startIndex);
if (index == -1)
{
MessageBox.Show("未找到");
index = f1.richTextBox1.Text.IndexOf(f1.richTextBox1.SelectedText);
}
else
{
f1.richTextBox1.Select(index, textBox1.Text.Length);//选中文本
index -= 1;
if (index < 0)
index = 0;
f1.Activate();
}
}
else
{
if (textBox1.Text.Length <= 0)
return;
index = f1.richTextBox1.Text.IndexOf(textBox1.Text, index);
if (index == -1)
{
MessageBox.Show("未找到");
index = f1.richTextBox1.Text.LastIndexOf(f1.richTextBox1.SelectedText) + 1;
}
else
{
f1.richTextBox1.Select(index, textBox1.Text.Length);//选择文本框中的文本
index += textBox1.Text.Length;//往下找
f1.Activate();
}
}
}
private void find_Load(object sender, EventArgs e)
{
f1 = (Form1)this.Owner;
index = f1.myindex;
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询