如何替换RichTextBox中的文本形式另一种形式? [英] How can I replace text in RichTextBox form another form?
问题描述
richTextBox1
的RichTextBox,名字为 XMLEditor
我希望能够重命名在富文本框的所有部分选择任何我想要的词。 (像记事本中的查找和替换)。但是我想使用另一个名为 Find
的表单在记事本中查找和替换)具有将替换 XMLEditor
中的 richTextBox1
中的单词的函数。
$ b
名为 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);
在构造Find时传递XMLEditor窗体作为参数,并且有一个公共的XMLEditor方法可以用于交互。
interface IFindAndReplace {
void Replace(String s);
$ b $ public class XMLEditor:IFindAndReplace {
...
public void ShowFindAndReplaceForm(){
Find findForm = new Find(this);
}
public void Replace(String s){
//替换方法
}
}
public class Find {
IFindAndReplace父;
public Find(IFindAndReplace parent){
this.parent = parent;
}
public Replace(String s){
parent.Replace(s);
//这将在父窗体上调用Replace。
编辑以使用接口:)
So I have a RichTextBox called richTextBox1
, in a form named XMLEditor
I want to be able to rename any chosen word in all parts of the rich text box with anything I want. (Like find and replace in notepad).
But I want to use another form called Find
(It looks like Find & Replace in notepad) to have functions that will replace the words in the richTextBox1
that is in XMLEditor
.
The form named Find
has a 2 textboxes and 1 button. The first textbox named textBox1
will be used to choose the text you will replace while textBox3
will be what the text is replaced with. And the button button3
will replace the text while clicked.
How can I replace text in a RichTextBox
from another form? How can I do this with these forms?
void button3_Click(object sender, EventArgs e)
{
XMLEditor xmle = new XMLEditor();
xmle.richTextBox1.Text = xmle.richTextBox1.Text.Replace(textBox1.Text, textBox3.Text);
}
解决方案 One thing you can do is pass the XMLEditor form as a parameter when constructing Find, and have a public XMLEditor method that can be used for interaction.
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.
}
}
edited to use interfaces :)
这篇关于如何替换RichTextBox中的文本形式另一种形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!