all groups > dotnet windows forms designtime > march 2005 >
You're in the

dotnet windows forms designtime

group:

need help passing data from form1 to form2


need help passing data from form1 to form2 E Stafford
3/17/2005 1:49:01 PM
dotnet windows forms designtime: 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.
Re: need help passing data from form1 to form2 Tim Wilson
3/17/2005 2:09:04 PM
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);

--
Tim Wilson
..Net Compact Framework MVP

[quoted text, click to view]

AddThis Social Bookmark Button