简体   繁体   English

如何替换另一种形式的RichTextBox中的文本?

[英]How can I replace text in RichTextBox form another form?

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. 因此,我有一个名为richTextBox1的RichTextBox,它的格式为XMLEditor我希望能够使用我想要的名称重命名RTF文本框所有部分中的所有选定单词。 (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 . 但是我想使用另一种称为“ Find形式(在记事本中看起来像“查找并替换”),以具有将替换richTextBox1XMLEditor中的XMLEditor

The form named Find has a 2 textboxes and 1 button. 名为“ Find ”的表单具有2个文本框和1个按钮。 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. 第一个名为textBox1文本框将用于选择要替换的文本,而textBox3将替换为该文本。 And the button button3 will replace the text while clicked. 按钮button3在单击时将替换文本。

How can I replace text in a RichTextBox from another form? 如何从另一种形式替换RichTextBox文本? 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. 您可以做的一件事是在构造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.
    }
}

edited to use interfaces :) 编辑使用界面:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM