Braintique.com header
Left Navigation Bar

Adding Code to the Event Handler

Finally, lets add code to the Button click event that causes the custom event to be fired, and to the form event handler that responds to the event.

Here’s code added to the Button click event that invokes the IsPrime method for the integers from 1 to 100:

private void btnDoPrimeThing_Click(object sender, System.EventArgs e) {
Class1 class1 = new Class1();
class1.OnPrime += new Class1.OnPrimeEventHandler (this.Class1_OnPrime);
for (long i=1; i <= 100; i++){
class1.IsPrime (i);
}
}

Finally, with a flourish of trumpets and a waving of flags, we need to add code to the event handler to be sure that our event is actually getting fired. Each prime number that fires the event is displayed in the TextBox:

private void Class1_OnPrime (object sender, System.EventArgs e,
long theNum){
textBox1.Text += theNum.ToString() + " is a prime\r\n";
}

Tip: As you may know, \r and \n are special characters that denote carriage return and new line respectively.

I’ve included the project discussed in this article as a downloadable zip archive should you want to inspect the full source code. (To install the project, unzip it into directory on a machine that is running Visual Studio, and—in Explorer—double-click the .sln file.)

Events are a very important part of programming in an object-oriented language such as C#, and can play a significant role in the architecture of projects. While there are quite a few details involved in programming events, if you keep the concepts, context and order of things clear, you shouldn’t have too many problems. I hope this article has helped clarify how to do this for you.

Previous Table of Contents Next


Google
 
Web www.braintique.com
www.digitalfieldguide.com www.googleplexblog.com


Home | Barticles | Blogs | Books | Services | FAQ | Contact

© Braintique.com. All rights reserved.

Search Engine Optimization







RSS 2.0 Syndication feed

Syndication Viewer

Our Web host:
IX WebHosting



Food for Your Brain! Get a Barticle! Questions Answered Books for You What We Can Do For You Contact Us Brain Food Questions Answered Books for You What We Can Do For You Frequently Asked Questions About Us Google Research Photoshop Wi-Fi and Wireless Networking The Natural Way to Write