Default Event Handlers vs. Non-Default Event Handlers

 

How Thumbnail1 (or Thumbnail2) Works

The main window of Thumbnail2 is shown in Figure 4-3:

image from book
Figure 4-3: Thumbnail2 main window

The data that the user enters in the Answer text box is checked after every keystroke, using lines TH016-TH028 in the Thumbnail1 listing. If the number entered is not a digit (0-9), the MessageBox in line TH025 reports the error to the user, and the user is expected to fix the data entry problem.

As soon as the user clicks on the Is my answer correct? button, the focus has left the text box, so lines TH040-TH043 execute. The number the user entered in the text box is moved to a permanent location (since the text box data will disappear in a few milliseconds ) in line TH042. An integer variable StIvesNumber has been declared in line TH013 (a variable global to the project) to hold the user s answer until it can be checked later.

The action moves to lines TH050-TH054 to check the user s answer. Since the only correct answer is 1, the code sets No into label9 if the answer is any number other than 1. If the user has entered 1, the text Yes is set into label9 for display.

To end the game the user clicks on the Quit button or on the X at the top right of the main window.

1.  

What happens if you leave out the return statement on line TH026?

Answers

1.  

This project works the same way, with or without the return statement. To try this out, comment out the line by placing two forward slashes just to the left of the return statement (while the project is loaded into the IDE). This tells the compiler that this is a comment line, not an executable statement.

Thumbnail1 (TH)

 Form1.cs TH001:        using System; TH002:        using System.Collections.Generic; TH003:        using System.ComponentModel; TH004:        using System.Data; TH005:        using System.Drawing; TH006:        using System.Windows.Forms;  TH009:        namespace Thumbnail1 TH010:        { TH011:          partial class Form1 : Form TH012:          { TH013:            public int StIvesNumber;  TH014:            public Form1() TH015:            {InitializeComponent();} //-----------------------------------------------------------------------------------------// TH016:            private void textBox1_TextChanged(object sender, EventArgs e) TH017:            { // Check that each character entered is a digit between 0 and 9. TH018:              int Stringlength = textBox1.Text.Length; TH019:              for (int jj = 0; jj < Stringlength; jj++) TH020:              { TH021:                if (Char.IsDigit(textBox1.Text[jj])) TH022:                  { } TH023:                else TH024:                { TH025:                  MessageBox.Show("Not a digit 0 - 9. Try Again!"); TH026:                  // return; TH027:                } TH028:              } TH029:            } //-----------------------------------------------------------------------------------------// TH040:            private void textBox1_Leave(object sender, EventArgs e) TH041:            { // Leaving textBox_1. Place data in a more permanent location. TH042:              StIvesNumber = Convert.ToInt32(textBox1.Text); TH043:            } //-----------------------------------------------------------------------------------------// TH050:            private void button1_Click(object sender, EventArgs e)                    TH051:            { // Is my answer correct? TH052:              if (StIvesNumber == 1) label9.Text = "Yes"; TH053:              else label9.Text = "No"; TH054:            } //-----------------------------------------------------------------------------------------// TH060:            private void button2_Click(object sender, EventArgs e) TH061:            { // Quit this program. TH062:              Close(); TH063:            } TH064:          } TH065:        } //=========================================================================================//  Form1.Designer.cs TH100:        namespace Thumbnail1 TH101:        { TH102:          partial class Form1 TH103:          {                    // Required designer variable. TH104:            private System.ComponentModel.IContainer components = null;                     // Clean up any resources being used. TH105:            protected override void Dispose(bool disposing) TH106:            { TH107:              if (disposing && (components != null)) components.Dispose(); TH108:              base.Dispose(disposing); TH109:            } TH110:            #region Windows Form Designer generated code                   // Required method for Designer support TH111:            private void InitializeComponent() TH112:            { TH113:              this.label1 = new System.Windows.Forms.Label(); TH114:              this.label2 = new System.Windows.Forms.Label(); TH115:              this.label3 = new System.Windows.Forms.Label(); TH116:              this.label4 = new System.Windows.Forms.Label(); TH117:              this.label5 = new System.Windows.Forms.Label(); TH118:              this.label6 = new System.Windows.Forms.Label(); TH119:              this.label7 = new System.Windows.Forms.Label(); TH120:              this.label8 = new System.Windows.Forms.Label(); TH121:              this.textBox1 = new System.Windows.Forms.TextBox(); TH122:              this.button1 = new System.Windows.Forms.Button(); TH123:              this.label9 = new System.Windows.Forms.Label(); TH124:              this.button2 = new System.Windows.Forms.Button(); TH125:              this.SuspendLayout();                     //                     // label1                     // TH126:              this.label1.AutoSize = true; TH127:              this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F,                       System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                       ((byte)(0))); TH128:              this.label1.Location = new System.Drawing.Point(80, 18); TH129:              this.label1.Margin = new System.Windows.Forms.Padding(3, 3, 3, 2); TH130:              this.label1.Name = "label1"; TH131:              this.label1.Size = new System.Drawing.Size(177, 17); TH132:              this.label1.TabIndex = 0; TH133:              this.label1.Text = "As I was going to Saint Ives,";                     //                     // label2                     // TH134:              this.label2.AutoSize = true; TH135:              this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F,                       System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                       ((byte)(0))); TH136:              this.label2.Location = new System.Drawing.Point(80, 36); TH137:              this.label2.Margin = new System.Windows.Forms.Padding(3, 1, 3, 1); TH138:              this.label2.Name = "label2"; TH139:              this.label2.Size = new System.Drawing.Size(187, 17); TH140:              this.label2.TabIndex = 1; TH141:              this.label2.Text = "I met a man with seven wives.";                     //                     // label3                     // TH142:              this.label3.AutoSize = true; TH143:              this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F,                       System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                       ((byte)(0))); TH144:              this.label3.Location = new System.Drawing.Point(80, 54); TH145:              this.label3.Margin = new System.Windows.Forms.Padding(3, 0, 3, 1); TH146:              this.label3.Name = "label3"; TH147:              this.label3.Size = new System.Drawing.Size(178, 17); TH148:              this.label3.TabIndex = 2; TH149:              this.label3.Text = "Every wife had seven sacks,";                     //                     // label4                     // TH150:              this.label4.AutoSize = true; TH151:              this.label4.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F,                       System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                       ((byte)(0))); TH152:              this.label4.Location = new System.Drawing.Point(80, 72); TH153:              this.label4.Margin = new System.Windows.Forms.Padding(3, 0, 3, 1); TH154:              this.label4.Name = "label4"; TH155:              this.label4.Size = new System.Drawing.Size(171, 17); TH156:              this.label4.TabIndex = 3; TH157:              this.label4.Text = "Every sack had seven cats,";                     //                     // label5                     // TH158:              this.label5.AutoSize = true; TH159:              this.label5.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F,                       System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                       ((byte)(0))); TH160:              this.label5.Location = new System.Drawing.Point(80, 90); TH161:              this.label5.Margin = new System.Windows.Forms.Padding(3, 0, 3, 1); TH162:              this.label5.Name = "label5"; TH163:              this.label5.Size = new System.Drawing.Size(157, 17); TH164:              this.label5.TabIndex = 4; TH165:              this.label5.Text = "Every cat had seven kits,";                     //                     // label6                     // TH167:              this.label6.AutoSize = true; TH168:              this.label6.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F,                       System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                       ((byte)(0))); TH169:              this.label6.Location = new System.Drawing.Point(80, 108); TH170:              this.label6.Margin = new System.Windows.Forms.Padding(3, 0, 3, 1); TH171:              this.label6.Name = "label6"; TH172:              this.label6.Size = new System.Drawing.Size(175, 17); TH173:              this.label6.TabIndex = 5; TH174:              this.label6.Text = "Kits, cats, sacks, and wives,";                     //                     // label7                     // TH175:              this.label7.AutoSize = true; TH176:              this.label7.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F,                       System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                       ((byte)(0))); TH177:              this.label7.Location = new System.Drawing.Point(80, 126); TH178:              this.label7.Margin = new System.Windows.Forms.Padding(3, 0, 3, 3); TH179:              this.label7.Name = "label7"; TH180:              this.label7.Size = new System.Drawing.Size(227, 17); TH181:              this.label7.TabIndex = 6; TH182:              this.label7.Text = "How many were going to Saint Ives?";                     //                     // label8                     // TH183:              this.label8.AutoSize = true; TH184:              this.label8.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F,                       System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                       ((byte)(0))); TH185:              this.label8.Location = new System.Drawing.Point(119, 160); TH186:              this.label8.Name = "label8"; TH187:              this.label8.Size = new System.Drawing.Size(72, 17); TH188:              this.label8.TabIndex = 7; TH189:              this.label8.Text = "Answer -->";                     //                     // textBox1                     // TH190:              this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; TH191:              this.textBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F,                       System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                       ((byte)(0))); TH192:              this.textBox1.Location = new System.Drawing.Point(185, 158); TH193:              this.textBox1.Name = "textBox1"; TH194:              this.textBox1.Size = new System.Drawing.Size(37, 21); TH195:              this.textBox1.TabIndex = 8; TH196:              this.textBox1.Leave += new System.EventHandler(this.textBox1_Leave); TH197:              this.textBox1.TextChanged += new                       System.EventHandler(this.textBox1_TextChanged);                     //                     // button1                     // TH198:              this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F,                       System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                       ((byte)(0))); TH199:              this.button1.Location = new System.Drawing.Point(119, 200); TH200:              this.button1.Name = "button1"; TH201:              this.button1.Size = new System.Drawing.Size(176, 23); TH202:              this.button1.TabIndex = 9; TH203:              this.button1.Text = "Is my answer correct?"; TH204:              this.button1.Click += new System.EventHandler(this.button1_Click);                     //                     // label9                     // TH205:              this.label9.AutoSize = true; TH206:              this.label9.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F,                       System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                       ((byte)(0))); TH207:              this.label9.Location = new System.Drawing.Point(187, 237); TH208:              this.label9.Name = "label9"; TH209:              this.label9.Size = new System.Drawing.Size(0, 0); TH210:              this.label9.TabIndex = 10;                     //                     // button2                     // TH211:              this.button2.Location = new System.Drawing.Point(299, 269); TH212:              this.button2.Name = "button2"; TH213:              this.button2.TabIndex = 11; TH214:              this.button2.Text = "Quit"; TH215:              this.button2.Click += new System.EventHandler(this.button2_Click);                     //                     // Form1                     // TH216:              this.AutoScaleDimensions = new System.Drawing.SizeF(5F, 13F); TH217:              this.ClientSize = new System.Drawing.Size(392, 306); TH218:              this.Controls.Add(this.button2); TH219:              this.Controls.Add(this.label9); TH220:              this.Controls.Add(this.button1); TH221:              this.Controls.Add(this.textBox1); TH222:              this.Controls.Add(this.label8); TH223:              this.Controls.Add(this.label7); TH224:              this.Controls.Add(this.label6); TH225:              this.Controls.Add(this.label5); TH226:              this.Controls.Add(this.label4); TH227:              this.Controls.Add(this.label3); TH228:              this.Controls.Add(this.label2); TH229:              this.Controls.Add(this.label1); TH230:              this.Name = "Form1"; TH231:              this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; TH232:              this.Text = "Thumbnail1"; TH233:              this.ResumeLayout(false); TH234:              this.PerformLayout(); TH235:            }   TH236:            #endregion   TH237:            private System.Windows.Forms.Label label1; TH238:            private System.Windows.Forms.Label label2; TH239:            private System.Windows.Forms.Label label3; TH240:            private System.Windows.Forms.Label label4; TH241:            private System.Windows.Forms.Label label5' TH242:            private System.Windows.Forms.Label label6; TH243:            private System.Windows.Forms.Label label7; TH244:            private System.Windows.Forms.Label label8; TH245:            private System.Windows.Forms.TextBox textBox1; TH246:            private System.Windows.Forms.Button button1; TH247:            private System.Windows.Forms.Label label9; TH248:            private System.Windows.Forms.Button button2; TH249:           } TH250:          } 
 


Unlocking Microsoft C# V 2.0 Programming Secrets
Unlocking Microsoft C# V 2.0 Programming Secrets (Wordware Applications Library)
ISBN: 1556220979
EAN: 2147483647
Year: 2005
Pages: 129

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net