471,321 Members | 1,860 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,321 software developers and data experts.

Writing text to another form

RSH
I have a basic framework where I have a frmMain which is my main form that
is opened when the application is run. Upon a button click I have a second
from that is opened as such:

Form FormB = new Form2();

FormB.Show();

That form has a label called "label1" which I would like to write to from
frmMain. I tried the somewhat logical (but inncorrect) syntax:

FormB.label1.text = "Success!";

Which doesn't work...I get a compile error:

Error 1 'System.Windows.Forms.Form' does not contain a definition for
'label1' \InitialData\Form1.cs 53 19 InitialData

What should I be doing here, and why doesn't this work the way one would
expect?

Thanks!

RSH



Dec 8 '05 #1
4 1761
Firstly, you've cast FormB as a Form - so you only have access to the
properties of a Form; try changing this to:

Form2 FormB = new Form2();

Second, I doubt that the actual label "label1" is marked as public, so you
probably can't access it directly.

The way to do this would be to add the following to Form2's declaration:

public string LabelText {
get {return label1.Text;}
set {label1.Text = value;}
}

And change your code to put FormB.LabelText = "Success!";

This uses a public property to avoid having to have direct access to a
private member variable.

Let me know if this helps,

Marc

"RSH" <wa*************@yahoo.com> wrote in message
news:Ob*************@tk2msftngp13.phx.gbl...
I have a basic framework where I have a frmMain which is my main form that
is opened when the application is run. Upon a button click I have a second
from that is opened as such:

Form FormB = new Form2();

FormB.Show();

That form has a label called "label1" which I would like to write to from
frmMain. I tried the somewhat logical (but inncorrect) syntax:

FormB.label1.text = "Success!";

Which doesn't work...I get a compile error:

Error 1 'System.Windows.Forms.Form' does not contain a definition for
'label1' \InitialData\Form1.cs 53 19 InitialData

What should I be doing here, and why doesn't this work the way one would
expect?

Thanks!

RSH


Dec 8 '05 #2
RSH wrote:

Instead of:
Form FormB = new Form2();

FormB.Show();

Try this:

Form2 FormB = new Form2();
FormB.Show();
FormB.label1.Text = "Success!";
Error 1 'System.Windows.Forms.Form' does not contain a definition for
'label1' \InitialData\Form1.cs 53 19 InitialData


That's because the System.Windows.Forms.Form does not have a label1
property. Your Form2 class does.

If you need to declare FormB as a generic Form then you can cast the
generic form to your form class like this:

Form FormB = new Form2();
FormB.Show();

(Form2) FormB.label1.Text = "Success!";
HTH

Chris

Dec 8 '05 #3
RSH
Perfect...That now makes perfect sense.

Thanks a lot!!!!

"Marc Gravell" <mg******@rm.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Firstly, you've cast FormB as a Form - so you only have access to the
properties of a Form; try changing this to:

Form2 FormB = new Form2();

Second, I doubt that the actual label "label1" is marked as public, so you
probably can't access it directly.

The way to do this would be to add the following to Form2's declaration:

public string LabelText {
get {return label1.Text;}
set {label1.Text = value;}
}

And change your code to put FormB.LabelText = "Success!";

This uses a public property to avoid having to have direct access to a
private member variable.

Let me know if this helps,

Marc

"RSH" <wa*************@yahoo.com> wrote in message
news:Ob*************@tk2msftngp13.phx.gbl...
I have a basic framework where I have a frmMain which is my main form that
is opened when the application is run. Upon a button click I have a second
from that is opened as such:

Form FormB = new Form2();

FormB.Show();

That form has a label called "label1" which I would like to write to from
frmMain. I tried the somewhat logical (but inncorrect) syntax:

FormB.label1.text = "Success!";

Which doesn't work...I get a compile error:

Error 1 'System.Windows.Forms.Form' does not contain a definition for
'label1' \InitialData\Form1.cs 53 19 InitialData

What should I be doing here, and why doesn't this work the way one would
expect?

Thanks!

RSH



Dec 8 '05 #4
RSH,

You could also add a new constructor to Form2 that accepts a string, which
you could then assign to the label.

Dave
Dec 9 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Neil Trigger | last post: by
6 posts views Thread by Sebastian Kemi | last post: by
12 posts views Thread by mammothman42 | last post: by
reply views Thread by Yunus's Group | last post: by
12 posts views Thread by LayneMitch via WebmasterKB.com | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.