C#
  Home arrow C# arrow Page 4 - How to Code and Test a Windows Forms Appli...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

How to Code and Test a Windows Forms Application
By: Murach Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 8
    2006-06-22

    Table of Contents:
  • How to Code and Test a Windows Forms Application
  • How to refer to properties, methods, and events
  • How an application responds to events
  • How to create an event handler for the default event of a form or control
  • The event handlers for the Invoice Total form

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    How to Code and Test a Windows Forms Application - How to create an event handler for the default event of a form or control


    (Page 4 of 5 )

    Although you can create an event handler for any event of any object, you're most likely to create event handlers for the default event of a form or control. So that's what you'll learn to do in this chapter. Then, in chapter 6, you'll learn how to create event handlers for other events.

    To create an event handler for the default event of a form or control, you double-click the object in the Form Designer. Then, Visual Studio opens the Code Editor, generates a method declaration for the default event of the object, and places the insertion point on a blank line between the opening and closing braces of that declaration. As a result, you can immediately start typing the C# statements that you want to include in the body of the method.

    To illustrate, figure 3-4 shows the code that was generated when I double-clicked the Calculate button on the Invoice Total form. In this figure, the code for the form is stored in a file named frmInvoiceTotal.cs. In addition, the name of the method is the name of the object (btnCalculate), an underline, and the name of the event (Click). The statement that wires the Click event of this button to this event handler is stored in the file named frmInvoiceTotal.Designer.cs.

    Before you start an event handler for a control, you should set the Name property of the control as described in chapter 2. That way, this name will be reflected in the method name of the event handler as shown in this figure. If you change the control name after starting an event handler for it, Visual Studio will change the name of the object in the event wiring, but it won’t change the name of the object in the method name. And that can be confusing when you’re first learning C#.

    You should also avoid modifying the method declaration that’s generated for you when you create an event handler. In chapter 6, you’ll learn how to modify the method declaration. But for now, you should leave the method declaration alone and focus on adding code within the body of the method.

    How to delete an event handler
     
    If you add an event handler by mistake, you can’t just delete it. If you do, you’ll get an error when you try to run the application. This error will be displayed in an Error List window as shown in figure 3-6, and it will indicate that the event handler is missing.

    The method that handles the Click event of the Calculate button


    Figure 3-4.  How to create an event handler for the default event of a form or control

    How to handle the Click event of a button

    1. In the Form Designer, double-click the control. This opens the Code Editor, generates the declaration for the method that handles the event, and places the cursor within this declaration.
    2. Type the C# code between the opening brace ({) and the closing brace (}) of the method declaration.
    3. When you are finished writing code, you can return to the Form Designer by clicking the View Designer button in the Solution Explorer window.

    How to handle the Load event for a form

    • Follow the procedure shown above, but double-click the form itself.

    Description

    • The method declaration for the event handler that’s generated when you double-click on an object in the Form Designer includes a method name that consists of the object name, an underscore, and the event name.
    • The event handler is stored in the cs file for the form.
    • Most the code that’s generated when you design a form, including the statement that wires the event to the event handler, is stored in the Designer.cs file for the form. If necessary, you can open this file to view or delete the event wiring.
    • In chapter 6, you’ll learn how to handle events other than the default event.

    That’s because when you create an event handler, Visual Studio also generates a statement that wires the event to the event handler. As a result, if you delete an event handler, you must also delete the wiring for the event. The easiest way to do that is to double-click on the error message in the Error List window. This will open the Designer.cs file for the form and jump to the statement that contains the wiring for the missing event handler. Then, you can delete this statement.

    More C# Articles
    More By Murach Publishing


       · This article is an excerpt from the book "Murach's C# 2005," published by Murach. We...
     

    Buy this book now. This article is excerpted from chapter three of the book Murach's C# 2005, written by Joel Murach (Murach, 2005; ISBN: 9781890774370). Check it out today at your favorite bookstore. Buy this book now.

    C# ARTICLES

    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT