C# User Control Events

Hi,

Ok So I have a user control which consists of a number of different elements, text boxes, and drop downs. What I would like todo is when ever any of the different elements are changed, or the on change event is triggered, a global on change event is triggered for the whole control. This way I can just see when anything inside the control is changed.

I cant seem to find how todo this, and havent done much event programming. Any help / pointers would be great.

Kind Regards

James
WJPSAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

silemoneCommented:
you could do an onchange event with javascript maybe...
it's hard to say..need more info..
0
Fernando SotoRetiredCommented:
Windows or Web application?
0
WJPSAuthor Commented:
Sorry, its a windows application.
0
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Fernando SotoRetiredCommented:
Hi WJPS;

Below is a code snippet of a User Control and a form that uses it. When you drag the user control on to the form set its Changed event as you would any other control.

Fernando
Lets say that this is the User Contro class
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace WindowsFormsApplication34
{
    public partial class MyUserControl : UserControl
    {
        public MyUserControl()
        {
            InitializeComponent();
        }
 
        // Define a delegate for the event to be raised
        public delegate void UserControlEventHandler(Object sender, String msg);
        // Define an event you want to catch
        public event UserControlEventHandler Changed;
 
        // The next three events raise the Changed event created above
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            // Check first to see if any one has registered for the event
            if (Changed != null)
                Changed(sender, " Text property has changed");
        }
 
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            // Check first to see if any one has registered for the event
            if (Changed != null)
                Changed(sender, " checked state has changed");
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            // Check first to see if any one has registered for the event
            if (Changed != null)
                Changed(sender, " button coltrol has been clicked");
        }
 
 
    }
}
 
This is the form that uses the User Control
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace WindowsFormsApplication34
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void myUserControl1_Changed(object sender, string msg)
        {
            Control ctl = (Control)sender;
            MessageBox.Show("The control " + ctl.Name + msg);
        }
    }
}

Open in new window

0
Fernando SotoRetiredCommented:
If you are not using .Net 3.5 you will need to pull the using statement:

using System.Linq;
0
AshleighGreenCommented:
I might have this wrong. But what I believe you want to do is detect a change inside the control from the form the control is in.

To do this inside the control you need to create an event:

public delegate void DataChangedEventHandler(object sender, EventArgs e);
public event DataChangedEventHandler DataChangedAction;

Then on each of these controls you need to add an onchange event.

ie:
private void InitializeComponent()
{
...
   txtMyTextBox.TextChanged += TriggerChangedEvent(sender, e);
   ddlMyDropDown.SelectedIndexChanged += TriggerChangedEvent(sender, e);
...
}

private void TriggerChangedEvent(object sender, EventArgs e)
{
   if(DataChangedAction!=null) //Ensures something is listening
   {
      DataChangedAction(sender, e)
   }
}
=======================
In your form/control holding this control you can add a listener for the DataChangedAction.

MyDataChangedControlName.DataChangedAction += DealWithTheAction(sender, e)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WJPSAuthor Commented:
Thanks for that,

Just want I was looking for. I need to test it, but this seems to look right anyway.

Regards

James
0
Fernando SotoRetiredCommented:
Hi WJPS;

Was there something wrong with my solution to this question?

Fernando
0
WJPSAuthor Commented:
Hi Fernando,

No nothing at all! I admit I should have possible shared the marks. It was just that Ashleighs was concise and to the point and I understood it instantly.

Sorry about that.

Kind Regards

James
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.