Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have designed two forms in c# project.
in form1 i have two buttons and in form2 have single button.
in form1 on button1_click opens form2. when we see form 2, I'm clicking on button of form2 on that click i want raise an event of form1's button2_click.

how can we.
thanks in advn.
Posted
Comments
Sergey Alexandrovich Kryukov 16-Jan-12 13:47pm    
Why? Always start such question with explaining why, not with technical means of doing it.
--SA

You would need to create an event on Form2 which Form1 would subscribe to. When Form1 receives the event it can invoke whatever processes or events are necessary. You don't want to couple the forms to tightly by adding specific actions or events from one form to another.
 
Share this answer
 
Comments
Govind K 16-Jan-12 13:46pm    
Hi Mark,
I'm a new so can you give me an example or any link where i can find it.
Not only you cannot raise an even in another form, you cannot raise (invoke) and event nowhere but in the class where the event is declared. This is an important limitation of events compared to regular delegate instances, an important fool-proof feature.

So, technically speaking, I would suggest to use a delegate instead of event, which would work just as well. However, I don't want to advise that. You see, this limitation for events is set for good reasons. As this is a fool-proof feature, it's important to not become a fool by applying some work-around to this limitation. :-)

The thing is: you are approaching to this problem from a wrong way. Instead of explaining the behavior you need, you are putting your vision of how you want to solve the problem in the question. That is not exactly a correct question; and you vision happens to be incorrect. You are almost loosing your change to get a proper advice by formulating a question in a wrong way.

I suppose you simply need to perform the same action with the first form when you press a button on this form, but also on the other form. First, let me tell you that this is a sign of bad design. Secondly, you should talk about the behavior, the action itself, as it has nothing to do with events. You are getting it exactly upside down. You need to do the opposite thing: you need to have an instance method of first form, so you can call this method from two different places: from some event handler in form one and another event handler in form two. Isn't that trivial?

Now, it leave for yet another problem: how to pass a reference to form one to the form two? Well, you can always do it, but won't it be nice or not? You might be passing too much access to another form. This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900