Project BattingPractice

 

Most complex project suites begin as a collection of simple programs, and at some point the programmers decide to write a top-level program that executes all the lower-level programs in the suite. To demonstrate how to write such a program, we display the top-level program in Keeping Track, the series of programs written earlier that enable a user to keep track of one s stocks, bonds , and funds. This is the Entry Point main window:

image from book
Figure 23-1: Entry Point window

This version of KT Entry Point is located in Visual Studio 2005\Projects\KT Source Code\KTEntryPoint. The project uses data files found in C:\Program Files\KT, a subdirectory that ProgramFiles.msi copied from the companion files (see the Introduction).

The seven buttons perform these tasks :

  • Startup [Add or Delete Portfolio Items] executes the Startup program. This program performs the tasks of describing one s financial portfolio in a file named Masterfile.dta.

  • Buy or Sold Declarations executes KTBuy_Sold, where all purchases and sales of financial documents are reported .

  • Cash In Portfolio is a record of the cash in one s portfolio. This amount of cash is added to the value of the stocks and bonds in the portfolio every time a net worth calculation is made.

  • Weekly Data Posting executes KTWeeklyDataPosting. In this program, each item listed in Masterfile emerges and the user is expected to enter the current value of each item. As soon as KTWeeklyDataPosting is complete, it invokes an internal program named NetWorth to declare the value of each item in the portfolio and the portfolio s total worth.

  • Print or Plot Historical Data prints or plots the net worth of the portfolio or the historical value of each item in the portfolio.

  • Masterfile Realphabetize realphabetizes the Masterfile.dta file so the items presented to the user are in alphabetical order.

  • Copy Data to Floppy copies the accumulated Keeping Track data onto a 1.4 M formatted floppy disk to ensure that no data is ever lost.

A partial listing of the KTEntryPoint program is shown at the end of this section. The complete project is located at Visual Studio 2005\Projects\KT Source Code\KTEntryPoint.

When the user clicks on the Cash In Portfolio button, this window appears:

image from book
Figure 23-2: Cash Assets in Portfolio window

The user is expected to enter a new cash value into the text box if this value has changed. To return to the main window, the user clicks on the Return button.

Place the project C:\Visual Studio 2005\Projects\KT Source Code\ KTEntryPoint into the IDE, compile it, and run it.

Notice that in line EP008 below, a new using statement must be included: using System.Diagnostics.

KTEntryPoint (EP) Listing

 Form1.cs: EP002:     using System; EP003:     using System.Collections.Generic; EP004:     using System.ComponentModel; EP005:     using System.Data; EP006:     using System.Drawing; EP007:     using System.Windows.Forms; EP008:     using System.Diagnostics;         // Start new process. EP009:     using System.IO;                  // 'FileExists', 'StreamReader'. EP010:     using System.Text;                // 'Encoding'. EP012:     namespace KTEntryPoint EP013:     { EP014:       partial class Form1 : Form EP015:       { EP016:         public string strCIP;         // Cash In Portfolio. EP017:         public Form1() EP018:         {InitializeComponent(); } //-----------------------------------------------------------------------------------------// EP020:         private void Form1_Load(object sender, EventArgs e) EP021:         { // Place 'Cash in Portfolio' number on the window.                  // Open file 'CashInPortfolio.dta'.                  // if (!File.Exists("C:\Program Files\KT\CashInPortfolio.dta")) EP022:           if (!File.Exists("CashInPortfolio.dta")) // Local address. EP023:           { EP024:             MessageBox.Show("Cannot open file 'CashInPortfolio.dta' to read."); EP025:           } EP026:           char[] charCIP = new char[9];                  // StreamReader wf = new StreamReader(                  // "C:\Program Files\KT\CashInPortfolio.dta", Encoding.ASCII); EP027:           StreamReader wf = new StreamReader(                  "CashInPortfolio.dta", Encoding.ASCII); // Local address. EP028:           wf.ReadBlock(charCIP, 0, 9); EP029:           strCIP = new string(charCIP); EP030:           wf.Close(); EP031:           label5.Text = strCIP; EP032:         } //-----------------------------------------------------------------------------------------// EP040:         private void button1_Click(object sender, EventArgs e) EP041:         { // 'Startup'. EP042:           ProcessStartInfo startinfo = new ProcessStartInfo(                  "C:\Program Files\KT\KTStartup.exe"); EP043:           Process.Start(startinfo); EP044:         } //-----------------------------------------------------------------------------------------// EP050:         private void button2_Click(object sender, EventArgs e) EP051:         { // Buy or Sold Declarations. EP052:           ProcessStartInfo startinfo = new ProcessStartInfo(                  "C:\Program Files\KT\KTBuy_Sold.exe"); EP053:           Process.Start(startinfo); EP054:         } //-----------------------------------------------------------------------------------------// EP060:         private void button3_Click(object sender, EventArgs e) EP061:         { // Cash in portfolio.                  // Open Form2. EP062:           Form2 frm2 = new Form2(); EP063:           frm2.ShowDialog(); EP064:           frm2.Close(); EP065:         } //-----------------------------------------------------------------------------------------// EP070:         private void button4_Click(object sender, EventArgs e) EP071:         { // Weekly Data Posting. EP072:           ProcessStartInfo startinfo = new ProcessStartInfo(                  "C:\Program Files\KT\KTWeeklyDataPosting.exe"); EP073:           Process.Start(startinfo); EP074:         } //-----------------------------------------------------------------------------------------// EP080:         private void button5_Click(object sender, EventArgs e) EP081:         { // Print or plot historical data. EP082:           ProcessStartInfo startinfo = new ProcessStartInfo(                  "C:\Program Files\KT\KTPlotter.exe"); EP083:           Process.Start(startinfo); EP084:         } //-----------------------------------------------------------------------------------------// EP090:         private void button6_Click(object sender, EventArgs e) EP091:         { // Masterfile realphabetize. EP092:           ProcessStartInfo startinfo = new ProcessStartInfo(                  "C:\Program Files\KT\KTRealphabetize.exe"); EP093:           Process.Start(startinfo); EP094:         } //-----------------------------------------------------------------------------------------// EP100:         private void button7_Click(object sender, EventArgs e) EP101:         { // Copy data to floppy. EP102:           ProcessStartInfo startinfo = new ProcessStartInfo(                  "C:\Program Files\KT\CopyToFloppy.exe"); EP103:           Process.Start(startinfo); EP104:         } //-----------------------------------------------------------------------------------------// EP110:         private void button8_Click(object sender, EventArgs e) EP111:         { // Quit. EP112:           Close(); EP113:         } EP114:       } EP115:     } //=========================================================================================// Form1.Designer.cs: [The complete 'tagged code' listing is located in App C -- KTVC#TaggedCode.doc] //=========================================================================================// Form2.cs: #region Using directives EP400:        using System; EP401:        using System.Collections.Generic; EP402:        using System.ComponentModel; EP403:        using System.Data; EP404:        using System.Drawing; EP405:        using System.Text;                      // 'Encoding'. EP406:        using System.Windows.Forms; EP407:        using System.IO;                        // 'StreamReader', 'StreamWriter'. EP408:        #endregion EP409:        namespace KTEntryPoint EP410:        { EP411:          partial class Form2 : Form EP412:          { EP413:            public string strCIP, strRB; EP414:            public int Stringlength;   EP415:            public Form2() EP416:            {InitializeComponent(); } //-----------------------------------------------------------------------------------------// EP420:            private void Form2_Load(object sender, EventArgs e) EP421:            { // Open file 'CashInPortfolio.dta' and place number on screen.                     // if (!File.Exists("C:\Program Files\KT\CashInPortfolio.dta")) EP422:              if (!File.Exists("CashInPortfolio.dta")) // Local address. EP423:              { EP424:                MessageBox.Show("Cannot open file 'CashInPortfolio.dta' to read."); EP425:              }                         // StreamReader wf = new StreamReader(                     // "C:\Program Files\KT\CashInPortfolio.dta", Encoding.ASCII); EP426:              StreamReader wf = new StreamReader(                  "CashInPortfolio.dta", Encoding.ASCII); // local address. EP427:           char[] charRB = new char[9]; EP428:           wf.ReadBlock(charRB, 0, 9); EP429:           strRB = new string(charRB); EP430:           wf.Close(); EP431:           label3.Text = strRB; EP432:         } //-----------------------------------------------------------------------------------------// EP440:         private void textBox1_TextChanged(object sender, EventArgs e) EP441:         {                  // New cash value.                  // Check that each char is a digit or a decimal point. EP442:           int NumberOfErrors = 0; EP443:           Stringlength = textBox1.Text.Length; EP444:           if (Char.IsDigit(textBox1.Text[Stringlength - 1]) || EP445:           textBox1.Text[Stringlength - 1] == '.') EP446:           { } EP447:           else NumberOfErrors++; EP448:           int NumberOfPeriods = 0; EP449:           for (int mm = 0; mm < Stringlength; mm++) EP450:           { EP451:             if (textBox1.Text[mm] == '.') NumberOfPeriods++; EP452:           } EP453:           if ((NumberOfPeriods > 1) || (NumberOfErrors > 0)) EP454:           { EP455:             MessageBox.Show("Not a number: Too many periods, a comma, or                                     bad digits. Try again."); EP456:           } EP457:           return; EP458:         } //-----------------------------------------------------------------------------------------// EP460:         private void textBox1_Leave(object sender, EventArgs e) EP461:         { EP462:           int DPF = 0;                         // Decimal point found. EP463:           int DPPosition = 0;                  // Decimal point position. EP464:           Stringlength = textBox1.Text.Length; EP465:           for (int bb = 0; bb < Stringlength; bb++) EP466:           { EP467:             if (textBox1.Text[bb] == '.')       // Decimal point found. EP468:             { EP469:               DPF++; EP470:               DPPosition = bb; EP471:             } EP472:           } EP473:           if (DPF == 0) // No decimal point found. EP474:           { EP475:             if (Stringlength == 1) strCIP = textBox1.Text + ".00     "; EP476:             else if (Stringlength == 2) strCIP = textBox1.Text + ".00    "; EP477:             else if (Stringlength == 3) strCIP = textBox1.Text + ".00   "; EP478:             else if (Stringlength == 4) strCIP = textBox1.Text + ".00  "; EP479:             else if (Stringlength == 5) strCIP = textBox1.Text + ".00 "; EP480:             else if (Stringlength == 6) strCIP = textBox1.Text + ".00"; EP481:           } EP482:           else                                     // Decimal point found. EP483:           { EP484:             if ((Stringlength - DPPosition) == 3) // Two digits after decimal point. EP485:             { EP486:               if (Stringlength == 4) strCIP = textBox1.Text + "     "; EP487:               else if (Stringlength == 5) strCIP = textBox1.Text + "    "; EP488:               else if (Stringlength == 6) strCIP = textBox1.Text + "   "; EP489:               else if (Stringlength == 7) strCIP = textBox1.Text + "  "; EP490:               else if (Stringlength == 8) strCIP = textBox1.Text + "  "; EP491:             } EP492:             else if ((Stringlength - DPPosition) == 2) // One digit after decimal                                                               // point. EP493:             { EP494:               if (Stringlength == 3) strCIP = textBox1.Text + "0     "; EP495:               else if (Stringlength == 4) strCIP = textBox1.Text + "0    "; EP496:               else if (Stringlength == 5) strCIP = textBox1.Text + "0   "; EP497:               else if (Stringlength == 6) strCIP = textBox1.Text + "0  "; EP498:               else if (Stringlength == 7) strCIP = textBox1.Text + "0 "; EP499:               else if (Stringlength == 8) strCIP = textBox1.Text + "0"; EP500:             } EP501:             else if ((Stringlength - DPPosition) == 1) // No digits after decimal                                                               // point. EP502:             { EP503:               if (Stringlength == 3) strCIP = textBox1.Text + "00    "; EP504:               else if (Stringlength == 4) strCIP = textBox1.Text + "00   "; EP505:               else if (Stringlength == 5) strCIP = textBox1.Text + "00  "; EP506:               else if (Stringlength == 6) strCIP = textBox1.Text + "00 "; EP507:               else if (Stringlength == 7) strCIP = textBox1.Text + "00"; EP508:             } EP509:           }                  // Open file 'CashInPortfolio.dta'.                  // if (!File.Exists("C:\Program Files\KT\CashInPortfolio.dta")) EP510:           if (!File.Exists("CashInPortfolio.dta"))     // local address. EP511:           { EP512:             MessageBox.Show("Cannot open file 'CashInPortfolio.dta' to write."); EP513:           }                  // 'false' below means 'overwrite'.                  // StreamWriter wg = new StreamWriter(                   // "C:\Program Files\KT\CashInPortfolio.dta", false,                  // Encoding.ASCII, 9); EP514:           StreamWriter wg = new StreamWriter( EP515:           "CashInPortfolio.dta", false, Encoding.ASCII, 9); // local address. EP516:           wg.Write(strCIP); EP517:           wg.Close(); EP518:         } //-----------------------------------------------------------------------------------------// EP520:         private void button1_Click(object sender, EventArgs e) EP521:         { // Return EP522:           Close(); EP523:         } EP524:       } EP525:     } //========================================================================================// Form2.Designer.cs: [The complete tagged code listing is located in App C -- KTVC#TaggedCode.doc] 
 


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