Project KT101DeploymentDemo

 

The Dialogs

The dialogs described in this section are as follows :

  • Open File dialog

  • Save File dialog

  • Folder Browser dialog

  • Font dialog

  • Color dialog

  • Print dialog

  • Print Preview dialog

The Open File dialog is the most common prebuilt dialog in the computer software industry. It is used to open a file.

image from book
Figure 22-1: Open File dialog

The code inside the event handler that opens this dialog looks like this:

 PB030:     private void button1_Click(object sender, EventArgs e) PB031:     { // Open file dialog. PB032:       OpenFileDialog openFile = new OpenFileDialog(); PB033:       openFile.DefaultExt = "doc";              // The Filter property requires a search string after the pipe ( | ). PB034:       openFile.Filter = "Word documents (*.doc) | *.doc"; PB035:       openFile.ShowDialog(); PB036:       if(openFile.FileNames.Length > 0) PB037:       { PB038:         foreach(string fileName in openFile.FileNames) PB039:         {                  // Insert code here to process the files. PB040:         } PB041:       } PB042:     } 

The Save File dialog, as its name suggests, is used to save a file.

image from book
Figure 22-2: Save File dialog

The code inside the event handler that opens this dialog looks like this:

 PB050:     private void button2_Click(object sender, EventArgs e) PB051:     { // Save file dialog. PB052:       SaveFileDialog saveFile = new SaveFileDialog(); PB053:       saveFile.ShowDialog(); PB054:     } 

The Folder Browser dialog allows the user to browse through a variety of folders.

image from book
Figure 22-3: Folder Browser dialog

The code inside the event handler that opens the Folder Browser dialog looks like this:

 PB060:     private void button3_Click(object sender, EventArgs e) PB061:     { // Folder browser dialog. PB062:       FolderBrowserDialog folderBrowser = new FolderBrowserDialog(); PB063:       folderBrowser.ShowDialog(); PB064:     } 

A number of font effects including style and size can be set in the Font dialog.

image from book
Figure 22-4: Font dialog

The code inside the event handler that opens the Font dialog looks like this:

 PB070:     private void button4_Click(object sender, EventArgs e) PB071:     { // Font dialog. PB072:       FontDialog fontDialog = new FontDialog(); PB073:       fontDialog.ShowDialog(); PB074:     } 

The Color dialog lets users select from a number of colors or create their own.

image from book
Figure 22-5: Color dialog

The code inside the event handler that opens the Color dialog looks like this:

 PB080:     private void button5_Click(object sender, EventArgs e) PB081:     { // Color dialog. PB082:       ColorDialog colorDialog = new ColorDialog(); PB083:       colorDialog.ShowDialog(); PB084:     } 

In the Print dialog, the user sets options to print his document.

image from book
Figure 22-6: Print dialog

The code inside the event handler that opens this dialog looks like this:

 PB090:     private void button6_Click(object sender, EventArgs e) PB091:     { // Print dialog. PB092:       PrintDialog printDialog = new PrintDialog(); PB093:       PrintDocument printdoc = new PrintDocument(); PB094:       printDialog.Document = printdoc; PB095:       printDialog.ShowDialog(); PB096:       if(printDialog.ShowDialog() == DialogResult.OK) PB097:       {                // Continue with printing. See Petzold, Programming Microsoft Windows                // with C#, pg 1030. PB098:       } PB099:     } 

The Print Preview allows you to see the print output on the screen.

image from book
Figure 22-7: Print Preview dialog

The code inside the event handler that opens this dialog and prints the page looks like this:

 PB160:     private void button7_Click(object sender, EventArgs e) PB161:     { // Print preview dialog.              // 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 'PreBuiltCode' folder, subdirectory 'bin\debug'. PB162:       FileStream fk; PB163:       try PB164:       { PB165:           fk = new FileStream("KTWeeklyNetWorth.dta",FileMode.Open,FileAccess.Read); PB166:         } PB167:         catch PB168:         { PB169:           MessageBox.Show("Cannot open 'KTWeeklyNetWorth.dta' to count records."); PB170:           return; PB171:         } PB172:         NONWs = (int) (fk.Length / 47); PB173:         fk.Close(); PB174:         if(NONWs == 0) PB175:         { PB176:           MessageBox.Show("KTWeeklyNetWorth.dta is an Empty File."); PB177:           return; PB178:         }                // Compute the number of pages to be printed (40 entries per page). PB179:         PageNo = 0; PB180:         BigLoop = 0; PB181:         int TempNONWs = NONWs; PB182:         while(TempNONWs > 0) PB183:         { PB184:           BigLoop++; PB185:           TempNONWs = TempNONWs - 40; PB186:         } PB187:         for (int ww = 0; ww < BigLoop; ww++) PB188:         { PB189:           PageNo++; PB190:           StartPoint = 40 * ww; PB191:           EndPoint = 40 * (ww + 1); PB192:           if (EndPoint > NONWs) EndPoint = NONWs; PB193:           PrintDocument printDoc = new PrintDocument(); PB194:           printDoc.PrintPage += new PrintPageEventHandler(OnPrintPage); PB195:           PrintPreviewDialog printpDialog = new PrintPreviewDialog(); PB196:           printpDialog.Document = printDoc; PB197:           printpDialog.ShowDialog();                  // See Petzold, Programming Microsoft Windows with C#, pp 1036 - 1043. PB198:           } PB199:         } PB200:       } PB201:     } //-----------------------------------------------------------------------------------------// PB110:     void OnPrintPage(object obj, PrintPageEventArgs ppea)            // Part of 'Print Preview'. PB111:     { // Create the 11 records at the top of the printed page. PB112:       string[] strRT = new string[]{"KT Weekly Net Worth","Item No","Year-Week",                "Total Worth","Total Cost","Gain / Loss","----------","---------------",                "---------------","--------------","---------------"}; PB113:       int[] Base3 = new int[] {660,0,400,800,1200,1600,0,400,800,1200,1600}; PB114:       int[] Y3 = new int[] {200,300,300,300,300,300,365,365,365,365,365}; PB115:       int[] Base6 = new int[] {1270,0,800,1600,2400,3200,0,800,1600,2400,3200}; PB116:       int[] Y6 = new int[] {160,350,350,350,350,350,460,460,460,460,460}; PB117:       int[] Data3 = new int[] {55,470,825,1200,1600}; PB118:       int[] Data6 = new int[] {100,910,1650,2400,3300}; PB119:       char[] charWNWBlock = new char[47]; PB120:       Graphics grfx = ppea.Graphics; PB121:       grfx.PageUnit = GraphicsUnit.Document; // 1/300th of an inch.              // This line sets up the printer for 600 DPI vertically. PB122:       grfx.PageScale = 0.5f; // 1/600th of an inch.              // Select a font. PB123:       Font font1 = new Font("Arial", 14); PB124:       Font font2 = new Font("Arial",10); PB125:       int ot = 580; // Printing Offset. PB126:       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'. PB127:       StreamReader qq = new StreamReader("KTWeeklyNetWorth.dta");              // Print the column headings. PB128:       if(PageNo == 1) PB129:       grfx.DrawString(strRT[0],font1,Brushes.Black,Base6[0]+ot,Y6[0]); PB130:       for(int ss = 1; ss < 11; ss++) // 11 elements of the page header. PB131:       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. PB132:       string[] strData = new string[5]; PB133:       int ItemNo = StartPoint + 1; PB134:       int LineEntry = 0; PB135:       for(int kk = StartPoint; kk < EndPoint; kk++) // Up to 40 records. PB136:       { PB137:         string strItemNo = (kk + 1).ToString(); PB138:         strData[0] = strItemNo; PB139:         qq.ReadBlock(charWNWBlock, 0, 47); PB140:         strWNWBlock = new string(charWNWBlock); PB141:         strData[1] = strWNWBlock.Substring(5,5);       // Year-Week. PB142:         strData[2] = strWNWBlock.Substring(11,11);     // TTWorth. PB143:         strData[3] = strWNWBlock.Substring(23,11);     // TTCost. PB144:         strData[4] = strWNWBlock.Substring(35,11);     // TTGainLoss. PB145:         for(int ww = 0; ww < 5; ww++)                  // 5 data items. PB146:         grfx.DrawString(strData[ww],font2,Brushes.Black,Data6[ww]+ot,                                580 + 120 * LineEntry); PB147:         LineEntry++; PB148:       } PB149:     } // End of 'OnPrintPage'. 

A complete copy of the source code that supports these dialogs (Open File, Save File, Folder Browser, Font, Color, Print, and Print Preview) is located in project PreBuiltCode at Visual Studio 2005\Projects\ DemosSourceCode and in App D -- DemoVC#TaggedList.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