Question about communication between forms

C

craig

I am working on a winforms app in which I have a main form that has an
outlook-style explorer bar on the left, and a client area that is used to
host other forms in response to a user clicking on one of the items in the
explorer bar. The various forms that are hosted in the client area are
located in seperate assemblies.

My question is this...

What would be the best way to facilitate communication between the client
forms and the main form? For instance, there are times when something
happens on one of the client forms which might require that the main form
update one of its controls, etc.

I could wire-up some events to handle this, or I can pass a reference to the
main form to each of the client forms so that the client forms can call
methods on the main form at the appropriate times. I realize that there is
now an application block that is designed to address this type of UI
functionality, but we didn't have the time to take on that kind of a
learning curve.

Any thoughts are appreciated!!!
 
J

Jeremy Williams

craig said:
I am working on a winforms app in which I have a main form that has an
outlook-style explorer bar on the left, and a client area that is used to
host other forms in response to a user clicking on one of the items in the
explorer bar. The various forms that are hosted in the client area are
located in seperate assemblies.

My question is this...

What would be the best way to facilitate communication between the client
forms and the main form? For instance, there are times when something
happens on one of the client forms which might require that the main form
update one of its controls, etc.

I could wire-up some events to handle this, or I can pass a reference to the
main form to each of the client forms so that the client forms can call
methods on the main form at the appropriate times. I realize that there is
now an application block that is designed to address this type of UI
functionality, but we didn't have the time to take on that kind of a
learning curve.

Any thoughts are appreciated!!!

A common approach is to define an interface that is used as the
communication bridge between the main form and the child forms. The main
form would implement this interface, and the client forms would call methods
on this interface to indicate the need for action by the main form.

The interface can be placed in its own assembly and be referenced by the
main form assembly and the child form assemblies. The interface ensures that
the child forms only call methods they are supposed to (they cannot close
the main form if you don't want them to), and you can change make changes to
the main form without affecting the child form assemblies (they are
loosely-coupled).
 
C

craig

This sounds like a decent approach, Jeremy. I used an approach similar to
what you are describing for some custom data binding code that we are using.

I am going to spend some time checking it out.

Thanks for taking the time to respond!!
Craig
 
S

Sijin Joseph

I had a similar application requirement, the way i did it was have to
interfaces, one which was implemented by the child forms and one that was
implemented by the parent form. The parent form interface was passed as an
argument in the ctors of all child forms. This facilitated two way
communication between the child and parent forms.
 
C

craig

Thanks Sijin. That is the same approach jeremy suggested. I spent some
time implementing it today and it seems to work well.

The only question I had as I was implementing it was...

Is there a way to pass a reference to just the main form interface to the
client forms rather than a reference to the main form itself? Right now, I
am passing a reference to the main form whenever I instantiate the client
forms:

ClientForm clientForm = new ClientForm(this);

The client form constructor accepts an argument of type IMainForm.
 
S

Sijin Joseph

It's better if you cast the parameter before passing it to the ClientForm

ClientForm clientForm = new CleintForm((IMainForm)this);
 
C

craig

Ahhhhh.....that is exactly what I was looking for!!!!!!!!!!!!! I wanted to
make sure that the client forms did not have the option of storing a
reference to the form rather than just the interface, but it never occurred
to me to do that cast.

THANKS!!!!!!!!!!!!!!!!
 

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