【发布时间】:2013-09-25 03:48:12
【问题描述】:
我在尝试替换与 rich text box 中特定单词匹配的所有文本时遇到问题。这是我使用的代码
public static void ReplaceAll(RichTextBox myRtb, string word, string replacer)
{
int index = 0;
while (index < myRtb.Text.LastIndexOf(word))
{
int location = myRtb.Find(word, index, RichTextBoxFinds.None);
myRtb.Select(location, word.Length);
myRtb.SelectedText = replacer;
index++;
}
MessageBox.Show(index.ToString());
}
private void btnReplaceAll_Click(object sender, EventArgs e)
{
Form1 text = (Form1)Application.OpenForms["Form1"];
ReplaceAll(text.Current, txtFind2.Text, txtReplace.Text);
}
这很好用,但是当我尝试用它自己和另一个字母替换一个字母时,我注意到了一个小故障。
例如,我想用ea 替换Welcome to Nigeria 中的所有e。
这就是我得到的Weaalcomeaaaaaaa to Nigeaaaaaaaaaaaaaaria。
当只有三个e 时,消息框会显示23。请问我做错了什么,我该如何纠正它
【问题讨论】:
标签: c# winforms replace richtextbox