c#.net help

Get help on programming - C++, Java, Delphi, etc.
Post Reply
po10cy
Registered User
Posts: 7160
Joined: 29 Jun 2004, 02:00
Location: Cape Town
Contact:

c#.net help

Post by po10cy »

lol i feel like such a noob, i still cant sort this problem out!!!

ok here it is

i have form1 and form2.

form 1 has a label, label1.
form 2 has a text box, textbox1, and a button, button1.

when the user enters data into textbox1 and clicks on button1, that data must go into label1 on form1.

in form2's code of clicking on button 1 i have:

form1 thisform = new form1();
thisform.label1.text = textbox1.text; //label 1 is set to public

that wont work.
ive tried making a new public string with get and set and it always brings a error.please some detailed help would be great.
when in doubt, paddle out... ;)
SBSP
Registered User
Posts: 3124
Joined: 09 May 2006, 02:00
Location: Centurion

Re: c#.net help

Post by SBSP »

po10cy wrote:lol i feel like such a noob, i still cant sort this problem out!!!

ok here it is

i have form1 and form2.

form 1 has a label, label1.
form 2 has a text box, textbox1, and a button, button1.

when the user enters data into textbox1 and clicks on button1, that data must go into label1 on form1.

in form2's code of clicking on button 1 i have:

form1 thisform = new form1();
thisform.label1.text = textbox1.text; //label 1 is set to public

that wont work.
ive tried making a new public string with get and set and it always brings a error.please some detailed help would be great.
I'm no c programmer or a programmer at all but arnt you doing it the wrong way round.


form1 thisform = new form1();
thisform.label1.text = textbox1.text; //label 1 is set to public
dont you have to put the code on form 2 and in place when you press the button it passes it to form1 label1

it seem like you have the code on form1 and your syntax might even be correct but the problem is when is lable1 going to be updated ?

unleas you have a loop running in form 1 updating lable1 all the time from form2's textbox ?


Something in the line of
form1.label1.text = thisform.textbox1.text
Last edited by SBSP on 08 May 2007, 16:01, edited 1 time in total.
po10cy
Registered User
Posts: 7160
Joined: 29 Jun 2004, 02:00
Location: Cape Town
Contact:

Post by po10cy »

calling all coders
when in doubt, paddle out... ;)
thealluseless
Registered User
Posts: 614
Joined: 29 Dec 2006, 02:00
Location: somewhere in the blight

Post by thealluseless »

rusty or judas could sort this out chop chop....but i battle to understand how u phrased it...if u post the code as it is written i think i can help?
I can picture in my mind a world without war, a world without hate. And I can picture us attacking that world, because they'd never expect it.
po10cy
Registered User
Posts: 7160
Joined: 29 Jun 2004, 02:00
Location: Cape Town
Contact:

Post by po10cy »

that code is in form2 and gets executed whenever the button get pressed....form1this form = new form1(); just allows me to call objects in form 1 (and label1 is in form1), so i have to say that to enable viewing whats in form1 ie: label1.

tnx tho.
when in doubt, paddle out... ;)
po10cy
Registered User
Posts: 7160
Joined: 29 Jun 2004, 02:00
Location: Cape Town
Contact:

Post by po10cy »

thealluseless wrote:rusty or judas could sort this out chop chop....but i battle to understand how u phrased it...if u post the code as it is written i think i can help?
ya its basically like that
when in doubt, paddle out... ;)
SBSP
Registered User
Posts: 3124
Joined: 09 May 2006, 02:00
Location: Centurion

Post by SBSP »

Last Question, then i will leave it to the pro's
"thisform" is that your own declaration or is it part of the built in syntax.

In VB for an example.

You can say Me.text1 = "Hello" or you can say Text1.text = "Hello" or Form1.text1 = "Hello"

So that made me think you had it on the wrong form.
cant you just say Form1.Label1 = form2.textbox1 after you did the whole
form1this form = new form1(); thing
Kronos
Moderator Emeritus
Posts: 4280
Joined: 28 May 2003, 02:00
Location: Azeroth
Contact:

Post by Kronos »

From where do you open the 2 forms?

The forms need to be aware of each other. So they both need a single point of origin, at least.
Image
po10cy
Registered User
Posts: 7160
Joined: 29 Jun 2004, 02:00
Location: Cape Town
Contact:

Post by po10cy »

form1 is the main form that starts when i create a new project, form 2 is just another form i added afterwards by clicking "add new component"...

form1 thisform = new form1(); means that i can say...thisform.label1.text...its a way i can access form1 from form2 by creating my own declaration of form1.
when in doubt, paddle out... ;)
User avatar
rustypup
Registered User
Posts: 8872
Joined: 13 Dec 2004, 02:00
Location: nullus pixius demonica
Contact:

Post by rustypup »

c# is not my cuppa, but both form1 and form2 must reside within some parent form or container. the hierarchy of visibility dictates that it is this parent who will get and set the attributes of it's contained components...

perhaps the best approach is MVC.
i) Model: Manages the data and notifies listeners about changes to said data
ii) View: Manages the GUI, attaching itself as a listener to the Model. You can have multiple views of the same data.
iii) Controller: Provides the user with access to modify the underlying Model's data.

So, when the user changes some portion of the data, using the Controller, the Model fires an event notifying its listeners, which in turn updates any Views which have been registered to detect the change.

am i making sense?
Most people would sooner die than think; in fact, they do so - Bertrand Russel
po10cy
Registered User
Posts: 7160
Joined: 29 Jun 2004, 02:00
Location: Cape Town
Contact:

Post by po10cy »

yes you are.

any code examples...
when in doubt, paddle out... ;)
Kronos
Moderator Emeritus
Posts: 4280
Joined: 28 May 2003, 02:00
Location: Azeroth
Contact:

Post by Kronos »

What rusty said...

and...

Remember that form1 is not static or global...
You can't just create a form1 object anywhere and expect the object to refer to your initial Application instance of form1. If you understand what I mean.

Remember that a form is just a class. And you create instances of it in your application.

Your main application method, (Through Application.Run()) creates an instance of the first form.
Image
po10cy
Registered User
Posts: 7160
Joined: 29 Jun 2004, 02:00
Location: Cape Town
Contact:

Post by po10cy »

so how do i do this? :oops:
when in doubt, paddle out... ;)
User avatar
rustypup
Registered User
Posts: 8872
Joined: 13 Dec 2004, 02:00
Location: nullus pixius demonica
Contact:

Post by rustypup »

ok...

first, create your data handler classes, with a view to GUI feedback, (iow, create the listener/event classes in tandem).

then create the controller classes, again with your eye on feedback.

finally, construct your view/s using the event handler mechanisms provided by the model... in this way your GUI instantiation will only require that a valid Model type be provided. The Model couldn;t care about what type of View is using it, and the Controller could'nt care about what type of Model is being used. this approach makes for more pluggable applications.

not to confuse you too much, your issue is one of scope. you need to insert a control point between form1 and form2 which has access to both instances. it is the control point which listens to the change happening in one and reflecting said change in the other.

you could, arguably, pass form1 as an instance reference into form2's constructor, assuming that form2 is an abstraction of form with an overridden constructor designed for the purpose... this is more of an OO question than a practical issue...

my waffle aside, you need a reference to an instance in order to access the object's members. If form1 creates form2, then form1 has a reference to form2. form2, however, doesn't have reference to form1, unless a reference is provided, either through form2's constructor or some method in form2 designed to accept a form1 instance.
Most people would sooner die than think; in fact, they do so - Bertrand Russel
po10cy
Registered User
Posts: 7160
Joined: 29 Jun 2004, 02:00
Location: Cape Town
Contact:

Post by po10cy »

I understand that makes sense.

thanks ill see if i can get it right at home tonight.
when in doubt, paddle out... ;)
HdB
Registered User
Posts: 929
Joined: 18 Oct 2004, 02:00
Location: Bloemfontein
Contact:

Post by HdB »

From where is form2 opened? When you click a button on form1?

Your code

Code: Select all

form1 thisform = new form1(); 
thisform.label1.text = textbox1.text; //label 1 is set to public
creates a NEW instance of form1 in form2. If you were to call thisform.Show() after the above code you'd open a new form1 with the label set to the value. So at that point you'll have 3 windows open.

If you open form2 from a button-click event on form1 your code will probably look something like

Code: Select all

form2 thisform = new form2();
thisform.show() // Could also be thisform.ShowDialog()
How do you open form2? Code like above, or something different?
AMD Athlon 64 X2 4800+ s939 | Gecube 3870 OC edition
Image
Post Reply