所以我有一个名为richTextBox1
的RichTextBox,名为XMLEditor
,我希望能够用我想要的任何内容重命名富文本框的所有部分中的任何选定单词。 (就像在记事本中查找和替换一样)。
但是我想使用另一个名为Find
的表单(看起来像在记事本中的查找和替换),以便具有替换richTextBox1
中XMLEditor
中的单词的函数。
名为Find
的表单有2个文本框和1个按钮。名为textBox1
的第一个文本框将用于选择要替换的文本,而textBox3
将替换为文本。按钮button3
将在点击时替换文本。
如何从其他表单中替换RichTextBox
中的文字?我怎么能用这些表格做到这一点?
void button3_Click(object sender, EventArgs e)
{
XMLEditor xmle = new XMLEditor();
xmle.richTextBox1.Text = xmle.richTextBox1.Text.Replace(textBox1.Text, textBox3.Text);
}
答案 0 :(得分:0)
您可以做的一件事是在构造Find时将XMLEditor表单作为参数传递,并且有一个可用于交互的公共XMLEditor方法。
interface IFindAndReplace {
void Replace(String s);
}
public class XMLEditor : IFindAndReplace {
...
public void ShowFindAndReplaceForm() {
Find findForm = new Find(this);
}
public void Replace(String s) {
//Replace method here
}
}
public class Find {
IFindAndReplace parent;
public Find(IFindAndReplace parent) {
this.parent = parent;
}
public Replace(String s) {
parent.Replace(s);
//this will call Replace on the parent form.
}
}
编辑使用接口:)