Beginner: Referencing a control on another form

D

Dave

I have two forms, Form1 and Form2. From Form2, how do I reference the value
in a control on Form1?

Or perhaps a more specific question: Form1 contains a textbox with the value
of 10. This form is hidden. Form2 is active and a user enters a value of 2
into a blank text box. How can I add this value to the textbox in Form1 so
that when Form1 is displayed it's textbox displays 12?

I probably have the wrong mindset here; I'm thinking Access where I could
set a reference to a control on another form. Is this the way it's done in
..NET? Or do I need to save everything to variables and refresh each form
when it is activiated?
 
G

Guest

Dave,
A cleaner design will be to create another class which manages your data.
Something based on the MVC pattern. You can find more information on this in
the User Interface Process Application Block on MSDN. You do not need to
implement everything from the Application Block. You can howerver pick up
some good pointers.


Regards,

Deepak
[I Code, therefore I am]
 
D

Dave

Thanks Deepak

I see a lot of good examples (Walkthroughs) but I haven't found one that is
right on point. But there is much available that I haven't gone through yet.

What I am trying to do is mimic an Access app that uses a switchboard to
call multiple forms that pass or reference values among themselves.

In short, I need to learn better the concepts of how forms should interact
and maintain state in a Windows .NET app.

If anyone knows of a good example, please let me know.



Deepak said:
Dave,
A cleaner design will be to create another class which manages your data.
Something based on the MVC pattern. You can find more information on this
in
the User Interface Process Application Block on MSDN. You do not need to
implement everything from the Application Block. You can howerver pick up
some good pointers.


Regards,

Deepak
[I Code, therefore I am]


Dave said:
I have two forms, Form1 and Form2. From Form2, how do I reference the
value
in a control on Form1?

Or perhaps a more specific question: Form1 contains a textbox with the
value
of 10. This form is hidden. Form2 is active and a user enters a value of
2
into a blank text box. How can I add this value to the textbox in Form1
so
that when Form1 is displayed it's textbox displays 12?

I probably have the wrong mindset here; I'm thinking Access where I could
set a reference to a control on another form. Is this the way it's done
in
..NET? Or do I need to save everything to variables and refresh each
form
when it is activiated?
 
S

Steven Cheng[MSFT]

Hi Dave,

As for the question you mentioned, I think Deepak's suggestion on apply a
MVC pattern is quite reasonable. For such scenario, we need to separate the
data with the displaying UI. Just use the more specific question you
mentioned:
Form1 has txt1 which display the total count

Form2 has txt2 which accept the user's new input

Then, Form1 and Form2 and all the controls on them are the UI, and the
actual data should be maintained in the application in separate place. We
can use a custom class (has a member variable ), for example named
"CountManager" to hold the total count. Then, when the user input new value
in the Form2's txt2 and click OK to submit it. We hide Form2 and update the
value in the custom class, just like:

CountManager.Total + = txt2.newValue;
Form2.Hide

Then, make Form1 visible, that'll fire the Form1's Activate event, and in
this event , we need to update the Form1's UI from the DAta
Store(CountManager.Total).

So this is a general progress of interaction between two forms. We can
abstract the progress as :

User change the UI(input) -----> update the data in data store(in memory
or other storage) ----------> fire event to Refresh UI ------> UI
retrieve updated data from data store and update it self.

Here is the reference on the UI process Application block in MSDN:

#User Interface Process Application Block for .NET
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/
uip.asp

Hope also helps. thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

Steven Cheng[MSFT]

You're welcome Dave,

Happy programming and enjoy .net :).

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
B

_BNC

... separate the
data with the displaying UI. Just use the more specific question you
mentioned:
...
Then, Form1 and Form2 and all the controls on them are the UI, and the
actual data should be maintained in the application in separate place. We
can use a custom class (has a member variable ), for example named
"CountManager" to hold the total count. Then, when the user input new value
in the Form2's txt2 and click OK to submit it. We hide Form2 and update the
value in the custom class, just like:
...
Then, make Form1 visible, that'll fire the Form1's Activate event, and in
this event , we need to update the Form1's UI from the DAta
Store(CountManager.Total).

A good explanation, to be sure. But I had posed a parallel question
recently. I normally use something analogous to a 'Facade' pattern, but
the main thing that bothered me was that when the number of controls
increases, it becomes impractical to pass the names of individual controls
to the module that is doing the data updates. In that case, you'd really
have to pass a pointer to the entire form.

Ex1: Two textboxes are being updated by a realtime data monitor. Fine...
just create a simple Facade that has pointers to those two controls. The
facade acts like a control panel. Pass a ref to the facade into the
module that retrieves data.

Ex2: But what if the form has 40 controls? The facade no longer
simplifies data flow, and in fact just adds another layer of complexity.
In that case it would seem 'logical' to pass a pointer to the entire form,
but that is not a clean design either, and in fact loses encapsulation.
 
S

Steven Cheng[MSFT]

Hi _BNC ,

Thanks for your join.
As for the problem you mentioned, based on my own understanding, when the
UI changed, it will notify the the model to change , this is done by call
some method on the controller or directory call a Management class's method
which has some parameters that contains the new input values. So that means
the controller or Management class should have no sense of the
UI(controls), they just use the input parameters in their method and use
those params to update the data store. How do you think ?

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top