A Simple, Programmer-Designed Main Menu

 

Project DisplayAFile

We will demonstrate some of the file display problems using code and a file from Keeping Track.

  1. Move to the desktop, click on the Visual Studio C# icon, and open the IDE. Click on File , followed by Open , then Project/Solution . When the Open Project window is displayed, move to directory Visual Studio 2005\Projects\DemosSourceCode\DisplayAFile, and double-click on project DisplayAFile to bring it into the IDE.

  2. Compile and run the project. The main window looks like this:

    image from book
    Figure 14-1: DisplayAFile main window

When the user clicks on the Display File button, this window appears:

Before Figure 14-2 could be executed, a layout for this display was created by a programmer in Form2 of this project. The layout looks like this:

image from book
Figure 14-2: View File ˜KTBuy.dta window
image from book
Figure 14-3: DisplayAFile layout

As we placed each label on the window template for Form2 we changed the label text from label22 (for example) to L22 to show that we had completed the placement of that label.

Form1 of this project opens file KTBuy.dta (lines DF0022-DF0031) and determines the number of records in the file (line DF0032). Lines DF0034-DF0044 determine how many times Form2 must be called to display all the records (NOReps) since only 15 records are displayed on each screen. Lines DF0045-DF0053 create Form2 NOReps times.

Note  

Note the comment between lines DF0024 and DF0025. In the Visual Studio C# compiler, if you place a local file to be read by the compiled program into the group of files in the project folder, the program cannot access the file. Instead, the file must be placed in a subdirectory two levels below the main directory ” in bin/debug with the executable file.

Partial DisplayAFile (DF) Listing

 Form1.cs: DF0002:       using System; DF0003:       using System.Collections.Generic; DF0004:       using System.ComponentModel; DF0005:       using System.Data; DF0006:       using System.Drawing; DF0007:       using System.Windows.Forms; DF0008:       using System.IO; // FileStream. DF0010:       namespace DisplayAFile DF0011:       { DF0012:         partial class Form1 : Form DF0013:         { DF0014:           public int kkForm1, NORecs; // NOReps; DF0015:           public Form1() DF0016:           { InitializeComponent();} //------------------------------------------------------------------------------------------// DF0020:           private void button1_Click(object sender, EventArgs e) DF0021:           { // Display file 'KTBuy.dta'.                     // Determine the number of records in 'KTBuy.dta'. DF0022:             FileStream fs; DF0023:             try DF0024:             {                       // Important: To read this 'local' file KTBuy.dta, you must place                       // the file in the 'DisplayAFile' folder, subdirectory 'bin\debug'. DF0025:               fs = new FileStream("KTBuy.dta", FileMode.Open, FileAccess.Read); DF0026:             } DF0027:             catch DF0028:             { DF0029:               MessageBox.Show("Cannot open 'KTBuy.dta' to count records."); DF0030:               return; DF0031:             } DF0032:             NORecs = (int) (fs.Length / 32); DF0033:             fs.Close(); DF0034:             int remainder = NORecs; DF0035:             int NOReps = 0; DF0036:             if(remainder <= 15) NOReps = 0; DF0037:             else DF0038:             { DF0039:               while(remainder >= 15) DF0040:               { DF0041:                 remainder = remainder - 15; // Fifteen records per page. DF0042:                 NOReps++; DF0043:               } DF0044:             } DF0045:             for(int kk = 0; kk <= NOReps; kk++) DF0046:             { DF0047:               kkForm1 = kk; DF0048:               // Create the Form2 modal window. DF0049:               Form2 frm2 = new Form2(kkForm1, NORecs); // Send 2 args to Form 2. DF0050:               frm2.ShowDialog(); DF0051:               frm2.Close(); DF0052:             } DF0053:           } //-----------------------------------------------------------------------------------------// DF0060:           private void button2_Click(object sender, EventArgs e) DF0061:           { // Quit. DF0062:             Close(); DF0063:           } DF0064:         } DF0065:       } //==============================================================// Form1.Designer.cs: DF0070:       namespace DisplayAFile DF0071:       { DF0072:         partial class Form1 DF0073:         {                   // Required designer variable. DF0074:           private System.ComponentModel.IContainer components = null;                   // Clean up any resources being used. DF0075:           protected override void Dispose(bool disposing) DF0076:           { DF0077:             if (disposing && (components != null)) components.Dispose(); DF0078:             base.Dispose(disposing); DF0079:           } DF0080:           #region Windows Form Designer generated code                   // Required method for Designer support. DF0081:           private void InitializeComponent() DF0082:           { DF0083:             this.label1 = new System.Windows.Forms.Label(); DF0084:             this.label2 = new System.Windows.Forms.Label(); DF0085:             this.label3 = new System.Windows.Forms.Label(); DF0086:             this.button1 = new System.Windows.Forms.Button(); DF0087:             this.button2 = new System.Windows.Forms.Button(); DF0088:             this.SuspendLayout();                     //                     // label1                     // DF0089:             this.label1.AutoSize = true; DF0090:             this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F,                       System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point,                       ((byte)(0))); DF0091:             this.label1.Location = new System.Drawing.Point(145, 11); DF0092:             this.label1.Name = "label1"; DF0093:             this.label1.Size = new System.Drawing.Size(209, 24); DF0094:             this.label1.TabIndex = 0; DF0095:             this.label1.Text = "Display File \'KTBuy.dta\'";                     //                     // label2                     // DF0096:             this.label2.AutoSize = true; DF0097:             this.label2.Location = new System.Drawing.Point(24, 50); DF0098:             this.label2.Name = "label2"; DF0099:             this.label2.Size = new System.Drawing.Size(433, 17); DF0100:             this.label2.TabIndex = 1; DF0101:             this.label2.Text = "This display consists of five columns of data. File                       \'KTBuy.dta\' contains";                     //                     // label3                     // DF0102:             this.label3.AutoSize = true; DF0103:             this.label3.Location = new System.Drawing.Point(24, 65); DF0104:             this.label3.Name = "label3"; DF0105:             this.label3.Size = new System.Drawing.Size(73, 17); DF0106:             this.label3.TabIndex = 2; DF0107:             this.label3.Text = "26 records.";                     //                     // button1                     // DF0108:             this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F,                       System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                       ((byte)(0))); DF0109:             this.button1.Location = new System.Drawing.Point(150, 89); DF0110:             this.button1.Name = "button1"; DF0111:             this.button1.Size = new System.Drawing.Size(200, 30); DF0112:             this.button1.TabIndex = 3; DF0113:             this.button1.Text = "Display File"; DF0114:             this.button1.Click += new System.EventHandler(this.button1_Click);                     //                     // button2                     // DF0115:             this.button2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F,                       System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                       ((byte)(0))); DF0116:             this.button2.Location = new System.Drawing.Point(212, 134); DF0117:             this.button2.Name = "button2"; DF0118:             this.button2.TabIndex = 4; DF0119:             this.button2.Text = "Quit"; DF0120:             this.button2.Click += new System.EventHandler(this.button2_Click);                     //                     // Form1                     // DF0121:             this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 16F); DF0122:             this.ClientSize = new System.Drawing.Size(484, 182); DF0123:             this.Controls.Add(this.button2); DF0124:             this.Controls.Add(this.button1); DF0125:             this.Controls.Add(this.label3); DF0126:             this.Controls.Add(this.label2); DF0127:             this.Controls.Add(this.label1); DF0128:             this.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F,                       System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                       ((byte)(0))); DF0129:             this.Name = "Form1"; DF0130:             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; DF0131:             this.Text = "Display A File"; DF0132:             this.ResumeLayout(false); DF0133:             this.PerformLayout(); DF0134:           } DF0135:           #endregion DF0136:           private System.Windows.Forms.Label label1; DF0137:           private System.Windows.Forms.Label label2; DF0138:           private System.Windows.Forms.Label label3; DF0139:           private System.Windows.Forms.Button button1; DF0140:           private System.Windows.Forms.Button button2; DF0141:         } DF0142:       } 

Form2 Sequence

Once Form2 has been created, the event handler Form2_Load is executed. Lines DF0194-DF0198 confirm that the file KTBuy.dta is present, and line DF0199 opens the file to read. This read always starts at the first record, so DF0201-DF0205 read and discard enough records to get to the records of interest. The serious reading of records begins at DF0205.

Notice that the record that is read from the file (DF0205) is read as a char, not as a string. ReadBlock only reads to a char buffer (32 chars). DF0204 takes the item number and prepares it for display in column one of Form2 (label12). Then lines DF0207-DF0211 parse the 32-char record into four variables (stock symbol, year-week of purchase, number of shares, and total price).

Lines DF0216-DF0221 place the data in the second row of the display (labels 17 through 21). But a new check must be made each time before the row is loaded: DF0213 checks to see if ItemNo is less than or equal to the number of records in the file (NOR). Once ItemNo is greater than NOR, the code fills the row of data with blanks (DF0225-DF0229). Every one of the labels (12 through 86) must be filled with something on each pass.

The code moves from the row 1 entries to the row 15 entries, and is finished. When the user is ready to see the next group of records, she clicks on the View the Next Fifteen Buys button. This button and the Quit button both return to Form1, but the Quit button exits the DF0045-DF0052 loop.

Partial Form2 Listing

 Form2.cs: DF0160:       #region Using directives DF0161:       using System; DF0162:       using System.Collections.Generic; DF0163:       using System.ComponentModel; DF0164:       using System.Data; DF0165:       using System.Drawing; DF0166:       using System.Text; DF0167:       using System.Windows.Forms; DF0168:       using System.IO; // File.Exists. DF0169:       #endregion DF0170:       namespace DisplayAFile DF0171:       { DF0172:         partial class Form2 : Form DF0173:         { DF0174:           public int RepNo, NOR; // Data sent from Form1. DF0175:           public Form2(int TNumber1, int TNumber2) DF0176:           { DF0177:             RepNo = TNumber1; // Data from Form1. DF0178:             NOR = TNumber2; // Data from Form1.                     // Required for Windows Form Designer support. DF0179:             InitializeComponent(); DF0180:           } //-----------------------------------------------------------------------------------------// DF0190:           private void Form2_Load(object sender, EventArgs e) DF0191:           { // Constructor. DF0192:             string empty = " "; DF0193:             label2.Text = NOR.ToString();                     // Open file 'KTBUY.DTA' to read the data. DF0194:             if (!File.Exists("KTBuy.dta")) DF0195:             { DF0196:               MessageBox.Show("Cannot find file 'KTBuy.dta' to read."); DF0197:               Close(); DF0198:             }                     // IMPORTANT: To read this 'local' file KTBuy.dta, you must place                     // the file in the 'DisplayAFile' folder, subdirectory 'bin\debug'. DF0199:             StreamReader w = new StreamReader("KTBuy.dta"); DF0200:             char[] charRealBlock = new char[33];                     // Read and discard as many records as necessary to place the file                     // pointer at the start of the proper record to read. DF0201:             if (RepNo != 0) DF0202:               for (int mm = 0; mm < 15 * RepNo; mm++) DF0203:               w.ReadBlock(charRealBlock, 0, 32);                     // Begin reading the file. DF0204:             int ItemNo=1+15*RepNo; // #1 DF0205:             w.ReadBlock(charRealBlock, 0, 32); DF0206:             label12.Text = ItemNo.ToString(); DF0207:             label13.Text = new string(charRealBlock, 0, 5); DF0208:             label14.Text = new string(charRealBlock, 6, 5); DF0209:             label15.Text = new string(charRealBlock, 12, 9); DF0210:             label16.Text = new string(charRealBlock, 22, 9); DF0211:             label9.Text = (NOR - ItemNo).ToString(); DF0212:             ItemNo++; // #2 DF0213:             if (ItemNo <= NOR) DF0214:             { DF0215:               w.ReadBlock(charRealBlock, 0, 32); DF0216:               label17.Text = ItemNo.ToString(); DF0217:               label18.Text = new string(charRealBlock, 0, 5); DF0218:               label19.Text = new string(charRealBlock, 6, 5); DF0219:               label20.Text = new string(charRealBlock, 12, 9); DF0220:               label21.Text = new string(charRealBlock, 22, 9); DF0221:               label9.Text = (NOR - ItemNo).ToString(); DF0222:             } DF0223:             else DF0224:             { DF0225:               label17.Text = empty; DF0226:               label18.Text = empty; DF0227:               label19.Text = empty; DF0228:               label20.Text = empty; DF0229:               label21.Text = empty; DF0230:             } [Repeat for items #3 through #15] DF0478:             w.Close(); // Close the file being read. DF0479:           } // End of Form2_Load(). //-----------------------------------------------------------------------------------------// DF0490:           private void button1_Click(object sender, EventArgs e) DF0491:           { // View the next fifteen buys. DF0492:             Close(); DF0493:           } //-----------------------------------------------------------------------------------------// DF0500:           private void button2_Click(object sender, EventArgs e) DF0501:           { // Quit. DF0502:             Close();  DF0503:           } DF0504:         } DF0505:       } //=========================================================================================// Form2.Designer.cs: DF0510:       namespace DisplayAFile DF0511:       { DF0512:         partial class Form2 DF0513:         {                   // Required designer variable. DF0514:           private System.ComponentModel.IContainer components = null;                   // Clean up any resources being used. DF0515:           protected override void Dispose(bool disposing) DF0516:           { DF0517:             if (disposing && (components != null)) components.Dispose(); DF0518:             base.Dispose(disposing); DF0519:           } DF0520:           #region Windows Form Designer generated code                   // Required method for Designer support. DF0521:           private void InitializeComponent() DF0522:           { DF0523:             this.button1 = new System.Windows.Forms.Button(); DF0524:             this.button2 = new System.Windows.Forms.Button(); DF0525:             this.label1 = new System.Windows.Forms.Label(); [Labels 2 through 86] DF0611:             this.SuspendLayout();                     //                     // button1                     // DF0612:             this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F,                       System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                       ((byte)(0))); DF0613:             this.button1.Location = new System.Drawing.Point(176, 370); DF0614:             this.button1.Name = "button1"; DF0615:             this.button1.Size = new System.Drawing.Size(161, 25); DF0616:             this.button1.TabIndex = 0; DF0617:             this.button1.Text = "View the Next Fifteen Buys"; DF0618:             this.button1.Click += new System.EventHandler(this.button1_Click);                     //                     // button2                     // DF0619:             this.button2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F,                       System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                       ((byte)(0))); DF0620:             this.button2.Location = new System.Drawing.Point(400, 370); DF0621:             this.button2.Name = "button2"; DF0622:             this.button2.Size = new System.Drawing.Size(75, 25); DF0623:             this.button2.TabIndex = 1; DF0624:             this.button2.Text = "Quit"; DF0625:             this.button2.Click += new System.EventHandler(this.button2_Click);                     //                     // label1                     // DF0626:             this.label1.AutoSize = true; DF0627:             this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F,                       System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                       ((byte)(0))); DF0628:             this.label1.Location = new System.Drawing.Point(16, 13); DF0629:             this.label1.Name = "label1"; DF0630:             this.label1.Size = new System.Drawing.Size(117, 14); DF0631:             this.label1.TabIndex = 2; DF0632:             this.label1.Text = "# of records in this file:"; [Labels 2 through 86]                     //                     // Form2                     // DF1313:             this.AutoScaleBaseSize = new System.Drawing.Size(7, 16); DF1314:             this.ClientSize = new System.Drawing.Size(529, 404); DF1315:             this.Controls.Add(this.label86); [Controls for Labels 85 through 1] DF1401:             this.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F,                       System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                       ((byte)(0))); DF1402:             this.Name = "Form2"; DF1403:             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; DF1404:             this.Text = "View File \'KTBuy.dta\'"; DF1405:             this.Load += new System.EventHandler(this.Form2_Load); DF1406:             this.ResumeLayout(false); DF1407:             this.PerformLayout(); DF1408:           } DF1409:           #endregion DF1410:           private System.Windows.Forms.Button button1; DF1411:           private System.Windows.Forms.Button button2; DF1412:           private System.Windows.Forms.Label label1; [Labels 2 through 86] DF1499:           private System.Windows.Forms.Label label86; DF1500:         } DF1501:       } 

A complete listing of this project may be found at Visual Studio 2005\Tagged KT-Demo Code\App D -- DemoVC#TaggedList.doc (DisplayAFile).

 


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