Click here to Skip to main content
15,910,411 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have custom control which has a panel in it. Inside this panel there is an another panel. This inner panel has a button.

Now I have to catch button click event in the first custom control. How to do it?

I am not creating button object in first panel or custom control. I am creating button object only in the second panel and I am adding this button object to this second panel
Posted
Updated 10-Jul-15 3:07am
v3

The problem formulation is not quite clear, but, based on many similar problems, this is a controversy between encapsulation of children of the custom or user control and the need for handling events dispatch to those children, without exposing the children themselves.

I could advice to handle (not "catch", handle) the events you need to expose internally and delegate them to other events accessible from outside. It would need a bit too much of extra work, but this is the whole idea of encapsulation of inner controls under the fasade of a composite user or custom control. This is a simple illustration of the idea:
C#
class MyUserControl : Control { // for example

    public MyUserControl() {
        // ...
        child.Click += (sender, eventArgs) => {
            if (ChildClick != null)
                ChildClick.Invoke(this, new System.EventArgs());
                // or, for example
                // ChildClick.Invoke(child, new System.EventArgs());
        } //permanent child.Click handler
    } //constructor

    // private, you don't expose it to the outside user:
    Button child = new Button(); 

    // ...

    public event System.EventHandler ChildClick;    

}

Are you getting the idea? Same things go with the events which have more specialized event arguments types (derived from System.EventArgs, your own type or predefined in .NET FCL). The you use
C#
public event System.EventHandler<concrete_event_args_type> SomeExternalEvent;

and transparently pass all data you carry in your event arguments type to the event invocation (second parameter of Invoke) for the child control's event.

If you have more than two levels of hierarchy in your custom or user control, you do the same, transparently passing events from the handler of inner control's event to its parent control. Just do the same work on more than one level.

—SA
 
Share this answer
 
v4
Comments
yash35 12-Jul-15 15:08pm    
So basically we have to handle click event of class 2 to class 1
and then pass that click event from class 1 to main user control.

so that we can HANDLE Smile | :) click event of class 2 in user control.

Is this what you wanted to convey me....?
Sergey Alexandrovich Kryukov 12-Jul-15 15:38pm    
Yes. The sample should explain everything. You can see how to encapsulate button, which is not exposed itself, but its event can be handled outside, by the user of MyUserControl. Now imaging that Button is itself a composite control, and you need to handle some event in a control insider it.
—SA
yash35 15-Jul-15 11:22am    
Thanks Sergey....It was much helpful
Sergey Alexandrovich Kryukov 15-Jul-15 11:27am    
You are very welcome.
Good luck, call again.
—SA
Dave Kreskowiak 19-Jul-15 22:53pm    
Really? "Dirty ship"? Whatever...

I'd rather have SA around here and not you. You haven't been around here for very long and you've already earned a reputation for being an ass.

Have a nice life.

You would normally handle the button event in your UserControl and then throw a new event from your UserControl that the main form subscribes to.

All the events for controls inside the UserControl should be handled by the UserControl code itself, not outside of it. If you need to expose events from these controls create your own events in your UserControl and raise them in the UserControl code as required. The form that uses the UserControl would then subscribe to these new events, not to the control events themselves.
 
Share this answer
 
Comments
yash35 10-Jul-15 10:37am    
Hi Dave, Thanks for reply.
But how do I handle that event in UserControl, if I don't have object of button in UserControl. Please write pseudo code for me.... :)
Dave Kreskowiak 10-Jul-15 10:45am    
The exact same way you do it in a form.
Sergey Alexandrovich Kryukov 10-Jul-15 14:19pm    
I think that the issue is the delegation of events dispatched to children to the upper parent level. Please see Solution 2.
—SA

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