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 DoSomeWor开发者_开发知识库k.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.
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
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;
精彩评论