下面的示例在 RichTextBox 的整个内容中搜索传递到此方法文本参数中的搜索字符串的第一个实例。搜索起始位置由此方法的起始参数指定。如果在 RichTextBox 中找到搜索字符串,则此方法返回找到文本的第一个字符的索引位置,并突出显示找到的文本;否则返回 -1 值。本示例还在搜索中指定匹配指定搜索字符串的大小写的选项。该示例假定此方法放置在 Form 的类中,并且该窗体包含一个名为 richTextBox1 的 RichTextBox。在找到搜索文本的第一个实例后,可以在执行“查找下一个”类型操作查找文本的其他实例时使用本示例。[Visual Basic, C#] 注意 此示例显示如何使用 Find 的一个重载版本。有关其他可用示例,请参阅单独的重载主题。 [C#] public int FindMyText(string text, int start) { // Initialize the return value to false by default. int returnValue = -1; // Ensure that a search string has been specified and a valid start point. if (text.Length > 0 && start >= 0) { // Obtain the location of the search string in richTextBox1. int indexToText = richTextBox1.Find(text, start, RichTextBoxFinds.MatchCase); // Determine whether the text was found in richTextBox1. if(indexToText >= 0) { returnValue = indexToText; } } return returnValue; } ******************************************************************至于替换,直接用string.replace()方法应该可以
{
try
{
if(F2.Visible)
{
F2.Activate ();
}
else
{
F2.Show ();
}
}
catch(Exception ex)
{
MessageBox.Show( ex.Message,"出错",MessageBoxButtons.OK ,MessageBoxIcon.Error);
}
}其中“Form2”有textBox1、textBox2、button1、button2,分别为“查找下一个”、“替换为”的文本框及按钮。在这我需要的是,“button1_Click”“button2_Click”中的代码!!我试着用以下代码实现,问题是不能高亮度显示已找到的文本(因为“Form2”没被激活),还有就是不能查找下一个(总是停留在第一个实例)
private void button1_Click(object sender, System.EventArgs e)
{
Form1 f1 = (Form1)this.Owner;
f1.richTextBox1.Find(this.textBox1.Text);
}
[C#]
public int FindMyText(string text, int start)
{
// Initialize the return value to false by default.
int returnValue = -1; // Ensure that a search string has been specified and a valid start point.
if (text.Length > 0 && start >= 0)
{
// Obtain the location of the search string in richTextBox1.
int indexToText = richTextBox1.Find(text, start, RichTextBoxFinds.MatchCase);
// Determine whether the text was found in richTextBox1.
if(indexToText >= 0)
{
returnValue = indexToText;
}
} return returnValue;
}
******************************************************************至于替换,直接用string.replace()方法应该可以