473,320 Members | 1,988 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 1829
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Neil Trigger | last post by:
Is there a way of creating a seperate text file on a server every time a form is sent? -- ¿ Trigger ? http://www.magic2k.com/ http://www.oddmap.co.uk
4
by: George Stout | last post by:
First off I do not know alot about writing queries to an Access Database from an ASP page. This is why I need help. I have an Events database for 6 colleges in our metro area. On the homepage I...
6
by: Sebastian Kemi | last post by:
How should a write a class to a file? Would this example work: object *myobject = 0; tfile.write(reinterpret_cast<char *>(myobject), sizeof(*object)); / sebek
12
by: mammothman42 | last post by:
hi ppl i've made a javascript component in a html file, and i want to be able to use this on other pages, so i'm going to put it into a js file. thing is, the component in 90% html and 10% java....
0
by: Yunus's Group | last post by:
Yunus's Group May 23, 3:36 pm show options Newsgroups: microsoft.public.dotnet.languages.vb From: "Yunus's Group" <yunusasm...@gmail.com> - Find messages by this author Date: 23 May 2005...
10
by: cj | last post by:
I'm having a problem writing an insert command to work with a datatable. I've looked at what the sqldataadapter creates and created my insert command to look the same. I have:...
1
by: ChrisFrohlich | last post by:
ASP.NET 2.0 with Text DataTypes: I've got a similar question going in the SQL group, but I was wondering if anyone has successfully implemented reading/writing character data from a Text datatype...
0
by: mcc99 | last post by:
Amazing, I surfed around to find a simple utility that could list files recursively from a given top folder down through it, writing out the path and filename in simple \path\file_name form, without...
12
by: LayneMitch via WebmasterKB.com | last post by:
Hello everyone. I'm currently learning Javascript and doing a few exercises. One problem I'm working on takes an array of names from an xml file using Ajax and writes it to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.