views:

35

answers:

1

Hi all,

maybe my design is not good, or I don't see the obvious solution, but I want to subscribe to a buttonClick EventHandler of Form1 from outside form1.

For example I have a Controller and Form1 who are both instanced in the main function. Now I want to subscribe a function from Controller to the buttonClick event from Button1_Click in Form1. But the button1 is declarded private, so i can't do

form1->Button1->Click += gcnew EventHandler(controller->function)

Is there any way to get around this?

Ok I could write a setter or something in Form1, but is there any other solution?

I read some examples, but they are all calling events from within the same class so they don't address my specific problem.

EDIT

Maybe it helps if I say what I really want to achieve:

Ok there is the GUI aka Form1 and a Conroller Class.

The Controller should get notified, if the user triggers a specific ButtonClick event on the GUI. Also the Controller should be able to subscribe and unsubscripe from different events during runtime. To make it even more confusing (atleast for me) the controller should raise events, which trigger some GUI behaviors, like enabling some buttons and disabling others. So this is what I want to do, at least in theory it sounded good, but now I have problems with the implementation.

+2  A: 

This is something you ought to refactor of course. Add an event to the Form1 class and let the button1's Click event raise the event.

Assuming this is difficult: there's a back-door through the public Controls property:

 form1->Controls["Button1"]->Click += // etc...
Hans Passant
Thanks. the back-door is working thanks.To you're first suggestion: I think this is not possible if I want to add or remove events during runtime? For example if a user makes a specific action, I want to subscribe to a new click event or unsubscribe an existing.
randooom
Well, why not? You remove event handlers with -=. Note that it is the client code that adds/removes event handlers, not Form1. Publishing and raising events is separate, that code is in Form1.
Hans Passant
Sorry, but I don't get it. Do I have to write an add(handler) and remove(handler) method inside of Form1 for each ButtonEvent? Sorry, but this is all very new stuff to me, so I am a little confused.
randooom
I gave a concrete Example of what I want to achieve in my post.
randooom
This is drifting far from your original question. You'll find lots of answers when you search this site for "MVC" and the winforms tag.
Hans Passant
Personally, I think it will raise more questions than it answers, but nevertheless thank you for the suggestion.
randooom
Don't want to sound harsh, it's just I'm really new to .Net and Design Patterns and all this stuff. So what may seem to answer questions for someone with experience in this fields, raises a bunch of questions upfront for me. :)
randooom
Finally I see what you said with your first suggestion, sorry for that :) It works wonderful and even solves another thing I had on my mind, thank u.
randooom