Do I need to include this project?

B

Brett Romero

How can I communicate with a parent form if that parent form type isn't
included in the project?

my.EXE project references a.DLL. a.DLL has a couple of forms that
my.exe uses. When my.exe opens a form in a.DLL, users can edit data in
that form. This data came out of a DataGrid on my.exe, which in turn
came from master.db. On the form in a.DLL, the user edits the data
and clicks update. A method in a.DLL saves to the DB. Now I need to
refresh the DataGrid in my.exe. What is the best way to do that?

Normally, I could cast the sender from a.DLL and refresh the grid in
my.exe. I can't do that here because my.exe isn't referencing a.DLL.
I'd like to keep a.DLL very generic, which means not including my.exe
as a reference, since the form in a.DLL can be called by any
application.

Any suggestions?

Thanks,
Brett
 
N

Nicholas Paldino [.NET/C# MVP]

Brett,

What you should do is define an interface in c.dll (another dll
basically) which the forms in a.dll implement. Then, you can cast the forms
to that interface, since both a.dll and the exe can reference c.dll (with
the interface in it).

The interface would expose a method that the form in the exe can
subscribe to, which would tell it to notify itself.

Hope this helps.
 
B

Brett Romero

That would mean in a.dll, the form I pass in will be of type c
(interface). From there, I should be able to reference the form,
assuming it is a property of the interface. For example:

c.Form1

The property defines Form1 as a type of myexe.Form1. Or do I need an
interface for each form? Why do you say I need to cast?

Thanks,
Brett
 
N

Nicholas Paldino [.NET/C# MVP]

Brett,

I am saying go the other way. When the EXE creates the form in a.dll,
have it cast the instance of the form created to your interface, and then
subscribe to the event exposed by it.
 
B

Brett Romero

Ok. Maybe I don't fully see it because I don't understand this part:

The interface would expose a method that the form in the exe can
subscribe to, which would tell it to notify itself.

Let's say the exposed event is the close button click in c.dll. Would
it look something similar to this:

[my.exe form1]
private I_cdll_interface someFormIn_ADLL;
this.someevent += new
someFormIn_ADLL.btnCloseEvent(this.Handlesomevent);

What event would I use in my.exe to and listen and take action? Where
would it be defined?

Thanks,
Brett
 
J

Jon Skeet [C# MVP]

Brett said:
Ok. Maybe I don't fully see it because I don't understand this part:

The interface would expose a method that the form in the exe can
subscribe to, which would tell it to notify itself.

Let's say the exposed event is the close button click in c.dll. Would
it look something similar to this:

[my.exe form1]
private I_cdll_interface someFormIn_ADLL;
this.someevent += new
someFormIn_ADLL.btnCloseEvent(this.Handlesomevent);

No - that would be subscribing to an event within the form in the EXE.
What event would I use in my.exe to and listen and take action? Where
would it be defined?

The event would be *declared* in the interface in the DLL. It would be
*implemented* by the forms in the DLL (and possibly by the forms in the
EXE, if you wanted). It would be *used* by the forms in the EXE, which
would then subscribe to the events using handlers which would probably
be in the EXE.

To change your example:

private I_cdll_interface someFormIn_ADLL = ...;

someFormIn_ADLL.someevent += new
someFormIn_ADLL.btnCloseEvent(this.Handlesomevent);

Jon
 
B

Brett Romero

The problem here is that any other application that uses c.dll will
also need a.dll right? As more DLLs use the interface, that's more
DLLs the apps will have to drag around.

Brett
 

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