?
Solved

C# User Control Events

Posted on 2008-11-17
9
Medium Priority
?
4,119 Views
Last Modified: 2013-12-17
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
Comment
Question by:WJPS
9 Comments
 
LVL 21

Expert Comment

by:silemone
ID: 22979279
you could do an onchange event with javascript maybe...
it's hard to say..need more info..
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 22979404
Windows or Web application?
0
 

Author Comment

by:WJPS
ID: 22979429
Sorry, its a windows application.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 64

Expert Comment

by:Fernando Soto
ID: 22979849
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
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 22979872
If you are not using .Net 3.5 you will need to pull the using statement:

using System.Linq;
0
 
LVL 2

Accepted Solution

by:
AshleighGreen earned 2000 total points
ID: 22980675
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
 

Author Closing Comment

by:WJPS
ID: 31517595
Thanks for that,

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

Regards

James
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 22984736
Hi WJPS;

Was there something wrong with my solution to this question?

Fernando
0
 

Author Comment

by:WJPS
ID: 22984833
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
Suggested Courses
Course of the Month17 days, 8 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question