Greetings,
I am having trouble trying to pass data from one form to another within
the same application.
My project is set up with a main form [Form1] and I added a second form
containing a richtextbox control using "add windows form" from the
solution explorer [Form2].
What I want to do is open Form2 from Form1 and then send data from a
richtext control named MessageErrors in Form1 to a RichText control
named errorText in Form 2.
From Form1 I used this code to open form2:
Form reports = new Form2();
reports.show();
The form opens up fine.
Now if I add the following code to Form2, I get the error: An Object
reference is required for the nonstatic field, method, or property
"app1.Form1.MessageErrors"
errorText.Text = Form1.MessageErrors.Text;
Fine, I need an object reference. So now I added:
Form1 form1 = new Form1();
errorText.Text = form1.MessageErrors.Text;
Now the application compiles without errors and I can call the function
to open Form2
But the errorText control in Form2 is blank and does not get the text
value from MessageErrors control in Form1.
I also tried using the following code in form1:
Form reports = new Form2();
reports.show();
reports.errorText.Text = messageErrors.Text;
but I get an error stating that:
'System.Windows.Forms.Form' does not contain a definition for errorText'
I am obvoiously missing something here.
I have searched the web and other forums for an answer without success.
Can anyone help me figure this out?
Tim Wilson - 17 Mar 2005 20:09 GMT
If you just need to pass the rich textbox text in to an instance of Form2,
and it doesn't need to be continuosly synced with the rich textbox on Form1,
then I would say create a constructor in Form2 that accepts a string
representing the text to place in the rich textbox. Something like this.
public Form2 (string displayData)
{
InitializeComponent();
this.richTextBox1.Text = displayData;
}
Then when you create an instance of Form2, use the constructor overload that
accepts a string.
Form2 f = new Form2(this.richTextBox1.Text);

Signature
Tim Wilson
.Net Compact Framework MVP
> Greetings,
> I am having trouble trying to pass data from one form to another within
[quoted text clipped - 42 lines]
> I have searched the web and other forums for an answer without success.
> Can anyone help me figure this out?