views:

124

answers:

2

I have a User Control for typical CRUD like actions on my WinForm app.
Validate, Insert, Update, Clear, Cancel, and Delete.

On every form I put this on I end up adding the click event, ucPersonNav.btnValidate.Click += new EventHandler(btnValidate_Click);, for every button.

What I am wondering is can I have the Events be on the User Control themselves and just have them point to a Method that I override on a Form by Form basis?

Something like this -->

namespace psUserControls
{

 using System;

using DevExpress.XtraEditors;

public partial class ucVIUCCDwithWhoDoneIt : XtraUserControl
{
    public ucVIUCCDwithWhoDoneIt()
    {
        InitializeComponent();
    }

    private void btnValidate_Click(object sender, EventArgs e)
    {
        ValidateEvent();
    }
}

}

And then on a Form have this -->

void ValidateEvent()
{
    if (dxValidDiagnosis.Validate())
    {
     if (planDiagnosisID != 0)
     {
      ucNavDiagnosis.btnUpdate.Enabled = true;
      ucNavDiagnosis.btnDelete.Enabled = true;
     }
     ucNavDiagnosis.btnInsert.Enabled = true;
    }
}

Is this feasible? Is it idiotic? If Yes then No then what steps do I need to take to make this work?

Thanks

A: 

You just need to define ValidateEvent as an event. In your UserControl:

public event EventHandler ValidateEvent;

On the form:

ucNavDiagnosis.ValidateEvent += new EventHandler(<name of event handler function>);

It's probably not a great idea to be accessing the buttons of the UserControl directly, however.

Adam Robinson
So, based on what you are showing me I wouldn't gain anything doing this. I would just be trading one thing for another....correct?
Refracted Paladin
That is what I'm guessing, unless there is work that can be refactored out and placed in the `UserControl` so it doesn't have to be duplicated on the form.
Adam Robinson
+2  A: 

i think .. not a bad idea.. but the approach would be very specific to your application.

we can have an enum for CRUD buttons - 6 enum items as you specified.

We can have a single event handler - delegate which takes above enum as a parameter. Write an event (MyButtonClickedEvent) for this delegate which will be fired on each button clicked event.

on your control, on each button clicked event you can fire this event with respective enum item as a parameter. e.g. on Validate button click, fire MyButtonClickedEvent with parameter as validate enum item. On Inser button click, fire same MyButtonClickedEvent with parameter as Insert enum item.

This way you will have to handle single event on your form. You will be firing different events from your control. But this is to be done only once. On your form you will write a just one single handler - Method. In this method, you can differentiate depending on the enum type. .Net supports enum in switch-case construct. So you can easily identify the opteration that you have to perform.

All the users of your control will find it easier as they have to handle just one event. They will ignore the cases in switch construct which they are not interested.

Hope this helps.