How can I replace text in RichTextBox form another form?

情到浓时终转凉″ 提交于 2019-12-12 02:27:18

问题


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);
}

回答1:


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 :)



来源:https://stackoverflow.com/questions/32688552/how-can-i-replace-text-in-richtextbox-form-another-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!