简体   繁体   中英

Adding text to RichTextBox in new form in

I have two forms, let's call them Main and Form2 .
Main form consists of a button and Form2 consists of a RichTextBox .

What I want my program to do is when I click button in the main form it calls function in class DoSomeWork.Do() . Function Do() gets some text from file, it has to open Form2 and paste this text to that RichTextBox .

Problem is that I don't know how to "access" this RichTextBox and paste text to it.

Thanks in advance.

In Form2 you add a method

public void InsertText(string text)
{
    richTextBox1.Text = text;
}

to use the method you open Form2 like this:

Form2 f2 = new Form2();
f2.InsertText("hello world");
f2.Show();

You can pass text values through Constructor .

Eg: create parameterised constructor for Form2

Public  Form2(string str)
       {
         this.Value=str;
         InitializeComponent();
        }

NOTE: Value is a public string in form Form2 .and you can set this value to richTextBox in form loading .

richTextBox1.Text=Value;

you can create a property on form 2

public string RichTextboxText 
{
    get
    {
        return this.RichTextBox1.Text
    }
    set
    {
        this.RichTextBox1.Text = value;
    }
}

Then create a new form:

Form2 f2 = new Form2() { RichtTextBoxText = "I like big butts"; } 
f2.Show();

Something like this should works

[edit]

just like to add that in this way you can also get the value back to from one.

in form one at any random point you can do:

string RichtEditTextFromForm2 = f2.RichTextBoxText;

given f2 is still active atleast

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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