Cs normally this is the only one you edit

This preview shows page 12 - 21 out of 48 pages.

Form1.cs : normally this is the only one you edit Form1.Designer.cs : holds the auto generated code Program.cs : contains the Main( ) method, where execution always begins Form1.cs and Form1.Designer.cs both include partial class definitions for the Form1 class
Image of page 12

Subscribe to view the full document.

C# Programming: From Problem Analysis to Program Design 13 Form Class A form a window Code in a class, FormName.cs (for example, Form1.cs) Reference and import System.Windows.Forms namespace Class heading definition Derived classes inherit from base class Includes not only the class name, but a colon followed by another class name Derived class (first class) Base class (second class) public class FormName : Form
Image of page 13
Inspecting the Code - Form1.cs Include namespace System.Windows.Forms Constructor calls InitializeComponent( ) method public partial class Form1: Form { public Form1( ) { // Required for Windows Form Designer support. InitializeComponent( ); } } This is the file where event handler methods will be placed C# Programming: From Problem Analysis to Program Design 14 Base class derived class
Image of page 14

Subscribe to view the full document.

Inspecting the Code - Form1.Designer.cs InitializeComponent( ) method included here # region Windows Form Designer generated code preprocessor directive // do not modify the contents of this method with the Code Editor Keyword “ this .” precedes property name Refers to current instance of the class #endregion // Ends the preprocessor directive C# Programming: From Problem Analysis to Program Design 15
Image of page 15
InitializeComponent( ) Method this.btnCalculate.Name = "btnCalculate"; this.btnCalculate.Size = new System.Drawing.Size(152, 72); this.btnCalculate.TabIndex = 4; this.btnCalculate.Text = "Calculate Take Home Pay"; this.btnCalculate.UseVisualStyleBackColor = false; this.btnCalculate.Click += new System.EventHandler(this.btnCalculate_Click); Some of the auto generated code in the method Added as default values for properties or from changing property values C# Programming: From Problem Analysis to Program Design 16
Image of page 16

Subscribe to view the full document.

C# Programming: From Problem Analysis to Program Design 17 Program.cs Execution begins in Main( ) method Main( ) is located in Program.cs file for the application Call to Run( ) method places application in process loop static void Main() { Application.EnableVisualStyles( ); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1( )); }
Image of page 17
C# Programming: From Problem Analysis to Program Design 18 Windows Form Events (in Form1.cs) Add code to respond to events , like button clicks Code goes into Form1.cs file From the Properties window, select the lightning bolt (Events) Double-click on the event name to generate code Registers the event as being of interest Adds a heading for event-handler method
Image of page 18

Subscribe to view the full document.

C# Programming: From Problem Analysis to Program Design 19 Windows Form Properties ( continued ) Events button selected Figure 9-6 Form1 events
Image of page 19
C# Programming: From Problem Analysis to Program Design 20 Windows Form – Closing Event In Form1.Designer.cs , code automatically added to register event this .FormClosing += new System.ComponentModel.CancelEventHandler ( this .Form1_FormClosing); In Form1.cs , code automatically added for method heading private void Form1_FormClosing(object sender, System.ComponentModel.CancelEventArgs e) { }
Image of page 20

Subscribe to view the full document.

Image of page 21
  • Winter '17
  • Qizhi Dai
  • Program design

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern