• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4128
  • Last Modified:

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
0
WJPS
Asked:
WJPS
1 Solution
 
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
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now