Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Downloads | Blogs | E-Books | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » Windows Forms » OnChanged Event for User Controls

OnChanged Event for User Controls

In this article I will show some ways how to create user controls with OnChanged event . The examples are written using C# for Windows applications.

Author Rank:
Technologies: .NET 1.0/1.1, Windows Forms,Visual C# .NET
Total downloads :
Total page views :  9202
Rating :
 4.67/5
This article has been rated :  3 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
 
Become a Sponsor


Related EbooksTop Videos

We often should know, whether were any changes on our form or not. And if such changes were, some process is carried out: just a message, some property has to be changed to "false", etc. There are no problems if we use "standard" controls (controls from the Toolbox: TextBox, ComboBox, etc), because every such control has OnChanged event (I mean TextChanged, SelectedIndexChanged, etc.) and we can catch every change. But... a little more interesting situation can be.

 

For example: we use UserControl1 that contains four TextBoxes, three ComboBoxes and UserControl2 that, in its turn, contains two TextBoxes and UserControl3 that, in its turn..., and so on; we have to catch any change on our form and when the number of changes of the UserControl2 will be equaled 2 we should "switch off" (enabled = false) this control.... In this case we have to "supply" the controls with event OnChanged.

 

In this article I will show some ways how to create user controls with OnChanged event. The examples are written using C# for Windows applications.

 

First of all let's divide our OnChanged events(which we are going to create) into two big parts: the events that have no event data and events that have.

 

The first case: The events that have no event data.

 

In this case in the code page of your user control you have to declare a delegate for OnChanged event and an event itself :

 

public delegate void Changed_EventHandler(object sender,EventArgs e);

public event Changed_EventHandler C_Changed;

 

But  .NET ( !!! ) have made part of this work: there is a method that handles the event that has no event data. And this is EventHandler Delegate. Using that you can replace (if you want) two lines of the above code by one:

 

public event EventHandler C_Changed;

 

Now you can add the next code

 

if (C_Changed != null) {C_Changed(this, e);}

 

to every OnChanged event of your control (again, saying "OnChanged event" I mean TextChanged, SelectedIndexChanged, etc.) and to force the control "to notice" each change occurring on it.

 

But more "elegant and programmer" way (if it is possible to say so) is use of a method, named OnC_Changed, that raises the event in all cases, that you need. In this case you can easily to use some method doOnChanged that executes the process logic for the C_Changed event :

 

protected virtual void OnC_Changed ( EventArgs e )               

{

if (C_Changed != null)

{

//Invokes the delegates.

C_Changed(this, e);

doOnChanged();

}

}

 

And now let's pass to practice and create some test project that has to have a Windows form with a user control "UC_TreeText". The last one consists of : textbox  "textBox_C1", textbox  "textbox _C2" with ReadOnly property set to "true" (just  to see total of changes of the UC_TreeTex control), user control "UC_TwoText". In its turn the UC_TwoText control consists of : textbox  "textBox_C1" ,  textbox  "textBox_C2", textbox  "textBox_C3" with ReadOnly property set to "true" (just  to see total of changes of the UC_TwoText control).

 

The process logic at changes of the UC_TwoText control is the following: at each change of the control color of the textBox_C3 control should vary and we should see total of changes of the UC_TwoText control. The process logic at changes of the UC_TreeText control is the following: at each change of the control we should see total of changes of the UC_TreeText control.

 

Our solution (named "WinProjects_Test") includes (by analogy) two projects: Output Type of the first project is "Windows Application" (named "OnChanged_UC") and Output Type of the second  is "Class Library" (named "OnChanged_UC_Controls"). The second project includes two User Controls: "UC_TreeText" and "UC_TwoText". The first project includes only a Windows form: "Form1".

 

The sequence of creation of our projects is the following.

 

After creating the solution with two projects add to the UC_TwoText control three textbox controls (with the names specified above). Double-click textBox_C1 and textBox_C2 to add TextChanged events and then add to the code page of the UC_TwoText control the next code:

 

#region "forEventChanged"

 

public event EventHandler C_Changed;           

                                                 

private int NumChanges = 0;//the number of changes

 

// The protected OnC_Changed method raises the event by invoking

// the delegates. In this case the sender is always this.

protected virtual void OnC_Changed(EventArgs e)                                          

{

if (C_Changed != null)

{

//Invokes the delegates.

C_Changed(this, e);                                           

doOnChanged();

}

}

//The method doOnChanged executes the process logic

//for "C_Changed" event.

private void doOnChanged()

{

//increase value of NumChanges by one

++NumChanges;

//changes BackColor to another color

if (textBox_C3.BackColor != Color.Yellow)

{

textBox_C3.BackColor = Color.Yellow ;

}

else

            {

                        textBox_C3.BackColor = Color.LightGreen ;

            }

            //write down and show the number of changes

            textBox_C3.Text = NumChanges.ToString();

}

#endregion

 

private void textBox_C1_TextChanged(object sender, System.EventArgs e)

{

            OnC_Changed(e);                    

}

 

private void textBox_C2_TextChanged(object sender, System.EventArgs e)

{

            OnC_Changed(e);                       

}

 

Now add to the UC_TreeText control two textbox controls (with the names specified above) and the UC_TwoText control. Double-click textBox_C1; then right-click on the UC_TwoText and select Properties, click "Events" button, double-click on "C_Changed". Now the UC_TreeText.cs code page are ready for adding a few lines of the following code:

 

#region "forEventChanged"

public event EventHandler C_Changed;

private int NumChanges =0;//the number of changes

 

// The protected OnC_Changed method raises the event by invoking

// the delegates. In this case the sender is always this.

protected virtual void OnC_Changed(EventArgs e)                              

{

            if (C_Changed != null)

            {

                        //Invokes the delegates.

                        C_Changed(this, e); 

                        doOnChanged();

            }

 

}

 

//The method doOnChanged executes the process logic

//for "C_Changed" event.

private void doOnChanged()

{

            ++NumChanges;

            textBox_C2.Text = NumChanges.ToString();

}

#endregion

 

private void textBox_C1_TextChanged(object sender, System.EventArgs e)

{

            OnC_Changed(e);

}

 

private void uC_TwoText1_C_Changed(object sender, System.EventArgs e)

{

            OnC_Changed(e);

}

 

Inside of the OnChanged_UC project from "My User Controls" tab drag and drop on the Form1 our UC_TreeText control.

 

That is all for the first part and you may test the project. Just start the project and be convinced that all our tasks (see above) are carried out.

 

The second case: The events that have event data.

 

Now we have to have some class that holds event data. In order to "facilitate" process to declare delegate for our OnChanged event and to create the event data class I recommend the following steps. Within the same namespace of the project of our user controls two classes have to be contained : a class for delegate declaration (public class OnChangedEvent) and a class that contains the data for the OnChanged event and derives from System.EventsArgs (public class C_EventArgs : EventArgs) .

 

To the OnChangedEvent class add the delegate declaration:

 

// Delegate declaration.

public delegate void Changed_EventHandler(object sender,C_EventArgs e); 

 

The C_EventArgs class may have a few constructors, properties, methods and has to  define some logic processes that corresponds to the certain changes of the certain control.

 

Now, to create OnChanged event in some user control of your project just add to the code page of this control:

 

// The event member that is of type Changed_EventHandler

//(class OnChangedEvent.cs).

public event OnChangedEvent.Changed_EventHandler C_Changed; 

 

To raise the event you use a method similar to the method considered above with at least one differences: you have to use "C_EventArgs":

 

// The protected OnC_Changed method raises the event by invoking

// the delegates. In this case the sender is always this.

protected virtual void OnC_Changed(C_EventArgs e)                         

{

            if (C_Changed != null)

            {

                        //you can set some properties here : e.[property_1];     

                        //Invokes the delegates.

                        C_Changed(this,e);                                            

                        doOnChanged ();

            }

}

 

Now we are going to pass to practice. Let's a little change our test project.

 

One of  the logical processes for our control, that we are going to use for our OnChanged event, is the following : after the certain quantity of changes of the control the last one has to change its "Enabled" property to "false" and to inform us on that (some message). Of course you can add some more processes, but for our example enough one process (and accordingly one "Constructor"). We are going to use this process only for the UC_TwoText control.

 

Add to the UC_TwoText  project class "OnChangedEvent.cs". Add to the code page class C_EventArgs and then add code for our logical process:

 

public class OnChangedEvent

{

            public OnChangedEvent()

            {

                        //

                        // TODO: Add constructor logic here

                        //

            }

            // Delegate declaration.

            public delegate void Changed_EventHandler(object sender,C_EventArgs e); 

}

//=========================================================

// Class that contains the data for

// the C_Changed event. Derives from System.EventArgs.

public class C_EventArgs : EventArgs

{

            private readonly int i_Changes = 0;

            private int i_NumChanges = 0;

            private Control o_Control ;

     

            //Constructor.

            public C_EventArgs(Control oControl, int iChanges)

            {

                        this.i_Changes = iChanges;

                        o_Control = oControl ;

            }

 

            // The C_Changes property returns the maximal number of changes ,

            // which can be made for the current control.

            public int C_Changes

            {    

                        get

                        {

                                    return i_Changes;

                        }     

            }

     

//The C_NumChanges property allows to set the number of changes

//( for the current control ) when the C_Changed event is generated.

//Here there is some logic process: to show a message and to change

//"Enable" property to "false" when the number of changes equel

//the maximal allowed number of changes . 

            public int C_NumChanges

            {

                        set

                        {

                                    i_NumChanges = value;

                                    if (i_Changes <= i_NumChanges + 1 &&

                                                                                     o_Control  !=   null)

                                    {

                                                MessageBox.Show (

" Enough changes for this control!","OnChanged_UC");

                                                o_Control.Enabled = false;

                                    }

                        }

            }

}

 

Now change the code page of the UC_TwoText control :

 

#region "forEventChanged"

// The event member that is of type Changed_EventHandler

//(class OnChangedEvent.cs).

public event OnChangedEvent.Changed_EventHandler C_Changed;            

private int NumChanges = 0;//the number of changes

 

// The protected OnC_Changed method raises the event by invoking

// the delegates. In this case the sender is always this.

protected virtual void OnC_Changed(C_EventArgs e) 

{

            if (C_Changed != null)

            {

                        e.C_NumChanges = NumChanges ;

                        //Invokes the delegates.

                        C_Changed(this,e);                                            

                        doOnChanged ();

            }

}

 

//The method doOnChanged executes the process logic

//for "C_Changed" event.

private void doOnChanged()

{

            //increase value of NumChanges by one

            ++NumChanges;

            //changes BackColor to another color

            if (textBox_C3.BackColor != Color.Yellow)

            {

                        textBox_C3.BackColor = Color.Yellow ;

            }

            else

            {

                        textBox_C3.BackColor = Color.LightGreen ;

            }

            //write down and show the number of changes

            textBox_C3.Text = NumChanges.ToString();

}

#endregion

 

private void textBox_C1_TextChanged(object sender, System.EventArgs e)

{

            //after five changes the control will be "swiched off"

            OnC_Changed(new C_EventArgs(this,5));

}

 

private void textBox_C2_TextChanged(object sender, System.EventArgs e)

{

                        //after five changes the control will be "swiched off"             

OnC_Changed(new C_EventArgs(this,5));

}

 

That is all for  the second part and you may test the project. Just start the project and be convinced that  all our tasks (see above for the second case) are carried out: every change of one of three our textboxes is followed by some logical process: writing down of the total of changes for every user control, changing of color and so on... and "switch off" one of our user controls after the certain quantity of its changes (we  have chosen 5; see Figure 1, Figure 2).

 

Figure 1.

 

Figure 2.

 

CONCLUSION

 

I hope that this article (with the detailed description of the test project ) will help you to choose the most suitable way when you need to "catch" changes, which occur on your user controls, and to "react" to this change according to necessary logic.

 

Good luck in programming !


Login to add your contents and source code to this article
 [Top] Rate this article
 About the author
 
Michael Livshitz
Michael is a lead programmer for a large technological firm. He has experience working with .NET projects (ASP.NET and Windows) since 2002. Currently used languages are C#, VB.NET, T-SQL and JavaScript. He also has experience working with C, Visual Basic, Oracle, SQL Server, and Access.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010
Microsoft Visual Studio 2010 offers more to developers than any other Visual Studio release. Work more productively and collaboratively-with greater control over your work at every step. The Beta 2 can give you a head start on achieving efficiency.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Become a Sponsor
 Comments
Can you give me one sample to understand easy! by Ba On January 5, 2009
Can you give me one sample to understand easy!
Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved