Accessing objects in different panels without passing references.

G

gwoodhouse

Morning,

My last question brought no end of fruit so i thought i would ask
this: Right now, i pass object references into the constructor of
forms i add to the panels in my application. Is there an easier way to
access objects/controls in other panels without implicily passing the
reference to them? Searching the internet, it seems not, but...

Thanks in advance again!

Graeme Woodhouse
 
J

JS

Is there an easier way to
access objects/controls in other panels without implicily passing the
reference to them? Searching the internet, it seems not, but...

I see this question asked a lot in this group. Without knowing
exactly what you're doing, I will suggest that you look into Model-
View-Controller (also called Model-View-Presenter) architectures.
Basically, storing application data inside controls is a bad idea.
You should separate data from user interface and if you do this you
should rarely need to access controls outside of the form/usercontrol
that they are on. MVC/MVP is a well-known set of patterns for dealing
with the interaction between data and user interface.
 
G

gwoodhouse

Thanks JS, but this isnt really an option. Most of what im writing is
simple interaction between control elements. ie, clicking a button in
one form adds a row to a table in a different form.
 
M

Marc Gravell

But that is just the point: host the data in a model (even
BindingList<T> would do the job!) and your button click just adds a
row to the list. The table can sort itself out based on notifications.

Marc
 
G

gwoodhouse

Being as this as the case i tried to set up some events.

Unfortunaly because they are on seperate forms the events can't see
each other! (This c# stuff is really providing a steep learning curve
for me today). Let me give you a simple example, hopefully should help
you understand what im trying to do.

Say i have a form which is split into two Panels.

On the first panel, you have several buttons, which clear Panel2 and
then adds a UserControl to the panel. (Each UserControl is a very very
large form with lots of its own complicated code). My problem is that
the code located in the UserControl cannot see, access or fire events
in my main method.

Am i programming in a very very faulty way, or am i missing a few
helpful ways to communicate between UserControls?

Thanks for all your advice so far!
 
J

JS

Unfortunaly because they are on seperate forms the events can't see
each other! (This c# stuff is really providing a steep learning curve

Try a static class or singleton class which holds all of your
application data. All of your controls will have access to this data
because it is static or singleton. This data class can also have
events that controls can subscribe to, so that when different things
happen in the application they can be notified. This way, controls
don't know about other controls -- they only know about themselves and
this application data object.
 

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