c#.net help
c#.net help
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.
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...
Re: c#.net help
I'm no c programmer or a programmer at all but arnt you doing it the wrong way round.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.
dont you have to put the code on form 2 and in place when you press the button it passes it to form1 label1form1 thisform = new form1();
thisform.label1.text = textbox1.text; //label 1 is set to public
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.
-
- Registered User
- Posts: 614
- Joined: 29 Dec 2006, 02:00
- Location: somewhere in the blight
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
"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
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.
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...
- rustypup
- Registered User
- Posts: 8872
- Joined: 13 Dec 2004, 02:00
- Location: nullus pixius demonica
- Contact:
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?
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
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.
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.
- rustypup
- Registered User
- Posts: 8872
- Joined: 13 Dec 2004, 02:00
- Location: nullus pixius demonica
- Contact:
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.
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
From where is form2 opened? When you click a button on form1?
Your code
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
How do you open form2? Code like above, or something different?
Your code
Code: Select all
form1 thisform = new form1();
thisform.label1.text = textbox1.text; //label 1 is set to public
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()