Click here to Skip to main content
15,885,953 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi I have created a user control containing a button. I want to know if I can call this user control on button click in the user control? if yes, how?

thanks,
Ananya
Posted

That's not exactly clear, but I'll try and explain the various possibilities for what you mean:

You can click the Button in the UserControl and raise an event in the UserControl which refers to the button: just add an event handler in the usual way and the sender parameter will contain the Button instance.

You can signal the event from a Button click to the outside world from the UserControl, but you have to create an Event within the UserControl first, and you should pass the UserControl instacne rather than the Button when you raise the event:
C#
/// <summary>
/// Event to indicate UserControl Button Clicked
/// </summary>
public event EventHandler Clicked;
/// <summary>
/// Called to signal to subscribers that UserControl Button Clicked
/// </summary>
/// <param name="e"></param>
protected virtual void OnClicked(EventArgs e)
    {
    EventHandler eh = Clicked;
    if (eh != null)
        {
        eh(this, e);
        }
    }
private void button1_Click(object sender, EventArgs e)
    {
    OnClicked(e);
    }


You can handle your own events within the UserControl (but why you would want to is another thing). Just add a handler for the event you created above in your constructor:

C#
Clicked += new EventHandler(MyUserControl_Clicked);
    ...
    }

void MyUserControl_Clicked(object sender, EventArgs e)
    {
    ...
    }

If this doesn't cover what you are trying to do, you need to explain in better detail.
 
Share this answer
 
Comments
Ananya Malik 6-Nov-13 0:02am    
actually my problem is a bit complicated. i have a winform where i have a groupbox , some comboboxes and a button. in the same project i have also created a usercontrol (GROUPBOX) with the same structure i.e. groupbox , comboboxes and a button.
when i press the button in the main form GROUPBOX gets displayed(which i what i want).
But when i press the button in that GROUPBOX, i want another GROUPBOX to be dispalyed below it. ie the button within the usercontrol must fire the usercontrol GROUPBOX again.
is this possible? if so how.
thanks
OriginalGriff 6-Nov-13 3:55am    
Don't try to do it like that - while it *is* possible, it means that the GROUPBOX has to be "aware" of it's parent and of the existence of other GROUPBOX instances, and that's a bad idea.

Instead, do as I suggested, and raise an event in GROUPBOX which is handled in the parent form. The parent then adds a new GROUPBOX if it decides it is needed. That way, each GROUPBOX remains independent, and only the form which contains them needs to know if there is more than one.

It also means that the parent controls where the GROUPBOX is displayed - if it wants them in a scrollable panel for example, it can do it with minor mods and the GROUPBOX doesn't need to know. If you add the new instance from inside the USerControl, then you have to assume (or impose) some structure and organisation on the parent form, which may not be appropriate.
Ananya Malik 6-Nov-13 4:46am    
oh alrite. actually i am already having what you are saying. from the parent form i am able to add GROUPBOXes . I just wanted to know if this thing is possible. thanks a lot.
OriginalGriff 6-Nov-13 4:52am    
It is - but it can cause a lot of headaches down the line. Better to do it properly in the first place (it normally ends up being quicker to implement as well)
Hi,

I am not clear about the requirements.

As per my understanding, you have a User Control and you have a Button in that control.

You need to Fire an event when the button is clicked inside the user control.

The Fired event should do some task with your Code Behind at the same time, needs to do some other event operation.

If this is your requirement, then go for "Delegates".

Create a anonymous function and on Button click do the normal code behenind operations also trigger a event to do the operations in the anonymous function.

Please let us know, if you need more details.



Thanks! C-sharp-event-handler-delegates[^]
 
Share this answer
 

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