Edit Controls

 

One of the major tasks that computer programs perform is that of printing hard copy for users. The print commands are straightforward, but there are so many options that one can easily miss the forest for all the trees. In this chapter we perform a small printing task ” printing columns of data taken from a file, KTWeeklyNetWorth.dta. To print in Visual Studio C# you must include the using System.Drawing.Printing statement at the top of the *.cs source code file.

Project PrintAFile

To display this project, open the IDE using the desktop icon, then click on File on the main menu, followed by Open and Project/Solution . When the Open Project window appears, move to the Visual Studio 2005\Projects\DemosSourceCode\PrintAFile directory and click on project PrintAFile in the list of demo projects. Compile and run the project.

A combo box is included on the main window so the user can choose any one of three printer fonts: Arial, Courier New, or Times New Roman. If you prefer other fonts you can change the project code easily. There is only one window in this project, as shown below:

image from book
Figure 16-1: Print a File window

When the program first opens, the Print File ˜KTWeeklyNetWorth.dta in this Font button is disabled (line PF032 in the listing below). Line PF033 lists the three possible fonts, and lines PF034-PF035 load the font names into comboBox1.

The user must click on the down arrow in the combo box to show the font names, and once she has clicked on one of them (which moves that font name to the top of the box), the Print File button is enabled (line PF142).

Once the Print File button has been clicked, the printing process begins:

  1. File KTWeeklyNetWorth.dta is opened in lines PF042-PF051 and the number of records in the file is determined in PF052 (NONWs).

  2. Computations are made in lines PF059-PF066 to determine the number of pages to be printed.

  3. Lines PF067-PF077 begin the printing process.

  4. In the OnPrintPage event handler (which is called in line PF074), computations are made in lines PF100-PF102 to scale the print on the page. By choosing GraphicsUnit.Document in line PF101, the vertical scale is set at 1/300th of an inch. When PageScale is set to 0.5f in line PF102, the vertical scale is now 1/600th of an inch (or 600 dots per inch, vertically).

  5. Lines PF103-PF104 create two fonts, based on the font name that the user picked in comboBox1. font1 is a 14-point font and font2 is a 10-point font (72 points to an inch).

  6. Line PF105 moves all the print 580 pixels to the right (measured from the left edge of the paper).

  7. Line PF107 opens the KTWeeklyNetWorth.dta file one more time (to read the data). Line PF109 prints the first line on the page in the larger font, font1, and lines PF110-PF111 print the remainder of the column labels in font2.

  8. Lines PF115-PF128 are the print loop. Line PF119 reads the file for new data, and lines PF121-PF124 parse the data into four variables (Year-Week, TTWorth, TTCost, and TTGainLoss). Lines PF125-PF126 put the data on the sheet of paper.

PrintAFile (PF) Listing

 Form1.cs: PF002:        using System; PF003:        using System.Collections.Generic; PF004:        using System.ComponentModel; PF005:        using System.Data; PF006:        using System.Drawing; PF007:        using System.Drawing.Printing; // Required to print. PF008:        using System.Windows.Forms; PF009:        using System.IO; // 'FileStream'. PF011:        namespace PrintAFile PF012:        { PF013:          partial class Form1 : Form PF014:          { PF015:            public int NONWs, BigLoop, StartPoint, EndPoint, PageNo; PF016:            public int[] Loop = new int[5]; PF017:            public char[] charDPI = new char[3]; PF018:            public Form1() PF019:            {InitializeComponent(); } //-----------------------------------------------------------------------------------------// PF030:            private void Form1_Load(object sender, EventArgs e) PF031:            { PF032:              button1.Enabled = false;                     // Place the font names and sizes into the comboBox. PF033:              string[] strFontNames = new string[] {"Arial", "Courier New",                       "Times New Roman"}; PF034:              for (int ii = 0; ii < 3; ii++) PF035:                comboBox1.Items.Add(strFontNames[ii]); PF036:            } //-----------------------------------------------------------------------------------------// PF040:            private void button1_Click(object sender, EventArgs e) PF041:            { // Print file 'KTWNW.dta' in this font.                     // Open file 'KTWeeklyNetWorth.dta' to count records.                     // IMPORTANT: To read this 'local' file 'KTWeeklyNetWorth.dta', you must                     // place the file in the 'PrintAFile' folder, subdirectory 'bin\debug'. PF042:              FileStream fk; PF043:              try PF044:              { PF045:                 fk = new FileStream("KTWeeklyNetWorth.dta", FileMode.Open,                                 FileAccess.Read); PF046:              } PF047:              catch PF048:              { PF049:                MessageBox.Show("Cannot open 'KTWeeklyNetWorth.dta' to count records."); PF050:                return; PF051:              } PF052:              NONWs = (int)(fk.Length / 47); PF053:              fk.Close(); PF054:              if (NONWs == 0) PF055:              { PF056:                MessageBox.Show("KTWeeklyNetWorth.dta is an Empty File."); PF057:                return; PF058:              }                     // Compute the number of pages to be printed (40 entries per page). PF059:              PageNo = 0; PF060:              BigLoop = 0; PF061:              int TempNONWs = NONWs; PF062:              while (TempNONWs > 0) PF063:              { PF064:                BigLoop++; PF065:                TempNONWs = TempNONWs - 40; PF066:              } PF067:              for (int ww = 0; ww < BigLoop; ww++) PF068:              { PF069:                PageNo++; PF070:                StartPoint = 40 * ww; PF071:                EndPoint = 40 * (ww + 1); PF072:                EndPoint > NONWs) EndPoint = NONWs; PF073:                PrintDocument prndoc = new PrintDocument(); PF074:                prndoc.PrintPage += new PrintPageEventHandler(OnPrintPage); PF075:                prndoc.Print(); PF076:              } PF077:            } //----------------------------------------------------------------------------------------// PF090:            void OnPrintPage(object obj, PrintPageEventArgs ppea) PF091:            { // Create the 11 records at the top of the printed page. PF092:              string[] strRT = new string[]{"KT Weekly Net Worth","Item No",                     "Year-Week","Total Worth","Total Cost","Gain / Loss","----------",                     "---------------","---------------","--------------","---------------"}; PF093:              int[] Base3 = new int[] { 660, 0, 400, 800, 1200, 1600, 0, 400, 800,                                               1200, 1600 }; PF094:              int[] Y3 = new int[] { 200, 300, 300, 300, 300, 300, 365, 365, 365, 365,                                            365 }; PF095:              int[] Base6 = new int[] { 1270, 0, 800, 1600, 2400, 3200, 0, 800, 1600,                                               2400, 3200 }; PF096:              int[] Y6 = new int[] { 160, 350, 350, 350, 350, 350, 460, 460, 460, 460,                                            460 }; PF097:              int[] Data3 = new int[] { 55, 470, 825, 1200, 1600 }; PF098:              int[] Data6 = new int[] { 100, 910, 1650, 2400, 3300 }; PF099:              char[] charWNWBlock = new char[47]; PF100:              Graphics grfx = ppea.Graphics; PF101:              grfx.PageUnit = GraphicsUnit.Document; // 1/300th of an inch.                     // This line sets up the printer for 600 DPI vertically. PF102:              grfx.PageScale = 0.5f; // 1/600th of an inch.                     // Place the user-selected font into the 'font' queue. PF103:              Font font1 = new Font(comboBox1.Text, 14); PF104:              Font font2 = new Font(comboBox1.Text, 10); PF105:              int ot = 580; // Printing offset. PF106:              string strWNWBlock;                     // Re-open file 'KTWeeklyNetWorth.dta'.                     // IMPORTANT: To read this 'local' file KTWeeklyNetWorth.dta, you must                     // place the file in the 'PrintAFile' folder, subdirectory 'bin\debug'. PF107:              StreamReader qq = new StreamReader("KTWeeklyNetWorth.dta");                     // Print the column headings. PF108:              if (PageNo == 1) PF109:                grfx.DrawString(strRT[0], font1, Brushes.Black, Base6[0] + ot, Y6[0]); PF110:              for (int ss = 1; ss < 11; ss++) // 11 elements of the page header. PF111:                 grfx.DrawString(strRT[ss], font2, Brushes.Black, Base6[ss] + ot,                                        Y6[ss]);                     // All the data to be printed is in record 'strWNWRecord'. The string is                     // parsed out into 4 substrings: strYearWeek, strTTWorth, strTTCost, and                     // strTTGainLoss. PF112:              string[] strData = new string[5]; PF113:              int ItemNo = StartPoint + 1; PF114:              int LineEntry = 0; PF115:              for (int kk = StartPoint; kk < EndPoint; kk++) // Up to 40 records. PF116:              { PF117:                string strItemNo = (kk + 1).ToString(); PF118:                strData[0] = strItemNo; PF119:                qq.ReadBlock(charWNWBlock, 0, 47); PF120:                strWNWBlock = new string(charWNWBlock); PF121:                strData[1] = strWNWBlock.Substring(5, 5);   // Year-Week. PF122:                strData[2] = strWNWBlock.Substring(11, 11); // TTWorth. PF123:                strData[3] = strWNWBlock.Substring(23, 11); // TTCost. PF124:                strData[4] = strWNWBlock.Substring(35, 11); // TTGainLoss. PF125:                for (int ww = 0; ww < 5; ww++)              // 5 data items. PF126:                grfx.DrawString(strData[ww], font2, Brushes.Black, Data6[ww] + ot,                             580 + 120 * LineEntry); PF127:                LineEntry++; PF128:              } PF129:            } // End of 'OnPrintPage'. //-----------------------------------------------------------------------------------------// PF140:            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) PF141:            { // Combo box. PF142:              button1.Enabled = true; PF143:            } //-----------------------------------------------------------------------------------------// PF150:            private void button2_Click(object sender, EventArgs e) PF151:            { // Quit. PF152:              Close(); PF153:            } PF154:          } PF155:        } //=========================================================================================// Form1.Designer.cs: PF170:        namespace PrintAFile PF171:        { PF172:          partial class Form1 PF173:          {                   // Required designer variable. PF174:            private System.ComponentModel.IContainer components = null;                   // Clean up any resources being used. PF175:            protected override void Dispose(bool disposing) PF176:            { PF177:              if (disposing && (components != null)) components.Dispose(); PF178:              base.Dispose(disposing); PF179:            } PF180:            #region Windows Form Designer generated code                   // Required method for Designer support. PF181:            private void InitializeComponent() PF182:            { PF183:              this.label1 = new System.Windows.Forms.Label(); PF184:              this.label2 = new System.Windows.Forms.Label(); PF185:              this.button1 = new System.Windows.Forms.Button(); PF186:              this.button2 = new System.Windows.Forms.Button(); PF187:              this.comboBox1 = new System.Windows.Forms.ComboBox(); PF188:              this.SuspendLayout();                     //                     // label1                     // PF189:              this.label1.AutoSize = true; PF190:              this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F,                       System.Drawing.FontStyle.Underline,System.Drawing.GraphicsUnit.Point,                       ((byte)(0))); PF191:              this.label1.Location = new System.Drawing.Point(195, 9); PF192:              this.label1.Name = "label1"; PF193:              this.label1.Size = new System.Drawing.Size(101, 24); PF194:              this.label1.TabIndex = 0; PF195:              this.label1.Text = "Print A File";                     //                     // label2                     // PF196:              this.label2.AutoSize = true; PF197:              this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F,                       System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                       ((byte)(0))); PF198:              this.label2.Location = new System.Drawing.Point(76, 51); PF199:              this.label2.Name = "label2"; PF200:              this.label2.Size = new System.Drawing.Size(115, 17); PF201:              this.label2.TabIndex = 1; PF202:              this.label2.Text = "Choose a Font -->";                     //                     // button1                     // PF203:              this.button1.Location = new System.Drawing.Point(110, 85); PF204:              this.button1.Name = "button1"; PF205:              this.button1.Size = new System.Drawing.Size(280, 25); PF206:              this.button1.TabIndex = 2; PF207:              this.button1.Text = "Print File \'KTWeeklyNetWorth.dta\' in this font"; PF208:              this.button1.Click += new System.EventHandler(this.button1_Click);                     //                     // button2                     // PF209:              this.button2.Location = new System.Drawing.Point(213, 124); PF210:              this.button2.Name = "button2"; PF211:              this.button2.Size = new System.Drawing.Size(75, 25); PF212:              this.button2.TabIndex = 3; PF213:              this.button2.Text = "Quit"; PF214:              this.button2.Click += new System.EventHandler(this.button2_Click);                     //                     // comboBox1                     // PF215:              this.comboBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F,                       System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                       ((byte)(0))); PF216:              this.comboBox1.FormattingEnabled = true; PF217:              this.comboBox1.Location = new System.Drawing.Point(204, 52); PF218:              this.comboBox1.Name = "comboBox1"; PF219:              this.comboBox1.Size = new System.Drawing.Size(121, 24); PF220:              this.comboBox1.TabIndex = 4; PF221:              this.comboBox1.SelectedIndexChanged += new                       System.EventHandler(this.comboBox1_SelectedIndexChanged);                     //                     // Form1                     // PF222:              this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); PF223:              this.ClientSize = new System.Drawing.Size(484, 162); PF224:              this.Controls.Add(this.comboBox1); PF225:              this.Controls.Add(this.button2); PF226:              this.Controls.Add(this.button1); PF227:              this.Controls.Add(this.label2); PF228:              this.Controls.Add(this.label1); PF229:              this.Name = "Form1"; PF230:              this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; PF231:              this.Text = "Print a File"; PF232:              this.Load += new System.EventHandler(this.Form1_Load); PF233:              this.ResumeLayout(false); PF234:              this.PerformLayout(); PF235:            } PF236:            #endregion PF237:            private System.Windows.Forms.Label label1; PF238:            private System.Windows.Forms.Label label2; PF239:            private System.Windows.Forms.Button button1; PF240:            private System.Windows.Forms.Button button2; PF241:            private System.Windows.Forms.ComboBox comboBox1; PF242:          } PF243:        } 
 


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