290 likes | 367 Views
Lecture 14:. File I/O Common Dialogs Toolbox Widgets. Introduction. OpenFileDialog and SaveFileDialog interact with the user to determine the location and name of a file to read or write. These common dialogs are not for the purpose of actually reading or writing files.
E N D
Lecture 14: • File I/O Common Dialogs Toolbox Widgets
Introduction OpenFileDialog and SaveFileDialog interact with the user to determine the location and name of a file to read or write. These common dialogs are not for the purpose of actually reading or writing files. Toobox Widgets support rapid-prototyping for the applications developer Reading and Writing textfiles is accomplished through StreamReader and StreamWriter. The foreach( ) looping construct can simplify the reading and writing of structured data.
Reading a Textfile array.txt 10 0 1 2 3 4 5 6 7 6 9 1 2 3 4 5 6 7 8 9 0 2 3 4 5 6 7 8 9 0 1 3 4 5 6 7 8 9 0 1 2 4 5 6 7 8 9 0 1 2 3 5 6 7 8 9 0 1 2 3 4 6 7 8 9 0 1 2 3 4 5 7 8 9 0 1 2 3 4 5 6 8 9 0 1 2 3 4 5 6 7 9 0 1 2 3 4 5 6 7 8 In the example code ReadTextArray a 10x10 array of integers named array.txt is read and loaded into a two-dimensional array.
Reading an Array of Integers string fname = ""; string textline = ""; fname = txtFilename.Text; int j; try { TextReader tr = newStreamReader(fname); n = Convert.ToInt32(tr.ReadLine()); txtDisplay.Text = Convert.ToString(n) + "\r\n"; W = newint[n,n]; for (int i = 0; i < n; i++) { textline = tr.ReadLine(); txtDisplay.Text = txtDisplay.Text + " " + textline + "\r\n"; j = 0; foreach (string subString in textline.Split(' ')) { if (subString != "") { W[i,j] = Int32.Parse(subString); j += 1; } } } tr.Close(); } catch { txtFilename.Text = "no such file"; }
Using the Array privatevoid btnCompute_Click(object sender, EventArgs e) { int sum; string str = ""; txtDisplay.Text = ""; txtDisplay.Text += "\r\n"; for (int i=0; i < n; i++) { sum = 0; for (int j=0; j < n; j++) { sum += W[i, j]; } str += "Row " + Convert.ToString(i) + " = " + Convert.ToString(sum) + "\r\n"; } txtDisplay.Text += str; }
The Benefit of Checking for Empty String array.txt 10 0 1 2 3 4 5 6 7 6 9 1 2 3 4 5 6 7 8 9 0 2 3 4 5 6 7 8 9 0 1 3 4 5 6 7 8 9 0 1 2 4 5 6 7 8 9 0 1 2 3 5 6 7 8 9 0 1 2 3 4 6 7 8 9 0 1 2 3 4 5 7 8 9 0 1 2 3 4 5 6 8 9 0 1 2 3 4 5 6 7 9 0 1 2 3 4 5 6 7 8
The Purpose of Common File Dialogs: to retreive a path and filename using System.IO; : : privatevoid OnFileOpen(object sender, EventArgs e) { OpenFileDialog dialog = newOpenFileDialog(); dialog.Filter = "text files (*.txt)|*.txt"; dialog.ShowDialog(); filename = dialog.FileName; OpenFile(); } privatevoid OnFileSaveAs(object sender, EventArgs e) { if (dlgSaveFile.ShowDialog() == DialogResult.OK) { filename = dlgSaveFile.FileName; SaveFile(); } }
Printing a Document with Images and Text try { printDocument.Print(); } catch (InvalidPrinterException ex) { MessageBox.Show(ex.Message, "Print Ticket", MessageBoxButtons.OK, MessageBoxIcon.Error); } : : privatevoid printDocument_PrintPage(object sender, PrintPageEventArgs e) { Bitmap center = newBitmap("ticket_header_center.jpg"); string line = ""; line = txtFirst.Text + " " + txtLast.Text; e.Graphics.DrawString(line, newFont("Arial", 12),Brushes.Black, 20, 240); line = txtStreet.Text; e.Graphics.DrawString(line, newFont("Arial", 12), Brushes.Black, 20, 260); line = txtCity.Text + ", " + txtState.Text + " " + txtZip.Text; e.Graphics.DrawString(line, newFont("Arial", 12), Brushes.Black, 20, 280); e.Graphics.DrawImage(center,275, 20); }
The Toolbox Widget Demo The Widget Demo is an example of an MDI application Each demo is presented as a Child Form under the Widget menu.
messageBox Demo publicpartialclassmsgBoxChild : Form { privateMessageBoxButtons buttonType = MessageBoxButtons.OK; privateMessageBoxIcon iconType = MessageBoxIcon.Exclamation; public msgBoxChild(WidgetDemo.widgetMain parent) { InitializeComponent(); this.MdiParent = parent; rbExclamation.Checked = true; rb_OK.Checked = true; } privatevoid btnDisplayMsgBox_Click(object sender, EventArgs e) { if (rb_OK.Checked) buttonType = MessageBoxButtons.OK; if (rb_OKCancel.Checked) buttonType = MessageBoxButtons.OKCancel; if (rb_AbortRetryIgnore.Checked) buttonType = MessageBoxButtons.AbortRetryIgnore; : : if (rbQuestion.Checked) iconType = MessageBoxIcon.Question; if (rbStop.Checked) iconType = MessageBoxIcon.Stop; if (rbWarning.Checked) iconType = MessageBoxIcon.Warning; DialogResult result = MessageBox.Show("Your message goes here.", "Custom MessageBox", buttonType, iconType); } }
Mouse Demo privatePoint mpt; public mouseChild(WidgetDemo.widgetMain parent) { InitializeComponent(); this.MdiParent = parent; } privatevoid mouseChild_MouseMove(object sender, MouseEventArgs e) { Point lpt = newPoint(); mpt = MousePosition; tbxXpos.Text = Convert.ToString(mpt.X); tbxYpos.Text = Convert.ToString(mpt.Y); Form mschild = this.ActiveMdiChild; lpt = this.Location; tbxrelXpos.Text = Convert.ToString(mpt.X - lpt.X - 6); tbxrelYpos.Text = Convert.ToString(mpt.Y - lpt.Y - 74); } privatevoid mouseChild_MouseDown(object sender, MouseEventArgs e) { MessageBox.Show(Convert.ToString(e.Button) + " button Pressed"); }
pictureBox Demo privatevoid newToolStripButton_Click(object sender, EventArgs e) { picBox.Image = null; this.Width = 200; this.Height = 200; } privatevoid openToolStripButton_Click(object sender, EventArgs e) { int minWidth = 100; int minHeight = 50; string fileName = ""; OpenFileDialog dlg = newOpenFileDialog(); if (dlg.ShowDialog() == DialogResult.OK) { fileName = dlg.FileName; Bitmap img = newBitmap(fileName); picBox.Image = img; if (img.Width + 10 > minWidth) this.Width = img.Width + 10; else this.Width = minWidth; if (img.Height + 60 > minHeight) this.Height = img.Height + 60; else this.Height = minHeight; } }
monthCalendar Demo privateDateTime dtobj = newDateTime(); public calChild(WidgetDemo.widgetMain parent) { InitializeComponent(); this.MdiParent = parent; } privatevoid monthCalendar_MouseUp(object sender, MouseEventArgs e) { dtobj = monthCalendar.SelectionStart; tbxDay.Text = Convert.ToString(dtobj.Day); tbxMonth.Text = Convert.ToString(dtobj.Month); tbxYear.Text = Convert.ToString(dtobj.Year); tbxDate.Text = Convert.ToString(dtobj.Date); tbxDayOfYear.Text = Convert.ToString(dtobj.DayOfYear); tbxDayOfWeek.Text = Convert.ToString(dtobj.DayOfWeek); }
linkLabel Demo privatevoid linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { linkLabel1.LinkVisited = true; System.Diagnostics.Process.Start(@"C:\"); } privatevoid linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { linkLabel2.LinkVisited = true; System.Diagnostics.Process.Start("notepad"); } privatevoid linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { linkLabel3.LinkVisited = true; System.Diagnostics.Process.Start("http://www.google.com"); }
privatevoid listBoxFavs_MouseUp(object sender, MouseEventArgs e) { rtbxSelected.Text = ""; foreach (string item in listBoxFavs.SelectedItems) { rtbxSelected.Text += item + "\n"; } }
privatestring[,] countrydata = newstring[237, 3]; privatevoid load_country_data() { string textline; int colnum; int rownum = 0; StreamReader tr = newStreamReader("country_data.txt"); do { textline = tr.ReadLine(); colnum = 0; foreach (string str in textline.Split('\t')) { if (str != "xxx") { countrydata[rownum, colnum] = str; colnum += 1; } else break; } rownum += 1; } while (textline != "xxx"); tr.Close(); } public comboChild(WidgetDemo.widgetMain parent) { InitializeComponent(); this.MdiParent = parent; load_country_data(); comboCountryList.Text = "Select a Country"; }
privatevoid comboCountryList_SelectedIndexChanged(object sender, EventArgs e) { int index = -1; try { index = comboCountryList.SelectedIndex; rtbDisplay.Text = "Selected index = " + Convert.ToString(index) + "\n"; rtbDisplay.Text += "Population = " + countrydata[index,2] + "\n"; rtbDisplay.Text += "Rank by Population = " + countrydata[index,1] + "\n"; rtbDisplay.Text += "Gross Domestic Product = " + countrydata[index,0] + "\n"; } catch { comboCountryList.Text = "Select a Country"; } }
tabControl Demo publicpartialclasstabChild : Form { privateint[] face = newint[4] { 0, 0, 0, 0 }; privateBitmap[] head = newBitmap[8]; privateBitmap[] eyes = newBitmap[8]; privateBitmap[] nose = newBitmap[8]; privateBitmap[] chin = newBitmap[8]; privatebool in_color = true;
publicvoid load_images() { string[] person = newstring[] {"churchill", "eastwood", "henryVIII", "joker", "mona", "monroe", "rubens", "vangogh"}; string filename; for (int i = 0; i < 8; i++) { filename = person[i] + "_01.jpg"; head[i] = newBitmap(filename); filename = person[i] + "_02.jpg"; eyes[i] = newBitmap(filename); filename = person[i] + "_03.jpg"; nose[i] = newBitmap(filename); filename = person[i] + "_04.jpg"; chin[i] = newBitmap(filename); } }
publicvoid show_face() { if (in_color) { picBoxHead.Image = head[face[0]]; picBoxEyes.Image = eyes[face[1]]; picBoxNose.Image = nose[face[2]]; picBoxChin.Image = chin[face[3]]; } else { Bitmap img0 = newBitmap(head[face[0]]); Grayscale(ref img0); picBoxHead.Image = img0; Bitmap img1 = newBitmap(eyes[face[1]]); Grayscale(ref img1); picBoxEyes.Image = img1; Bitmap img2 = newBitmap(nose[face[2]]); Grayscale(ref img2); picBoxNose.Image = img2; Bitmap img3 = newBitmap(chin[face[3]]); Grayscale(ref img3); picBoxChin.Image = img3; } }
privatevoid btnDisplay_Click(object sender, EventArgs e) { if (rbChurchill_01.Checked) face[0] = 0; if (rbEastwood_01.Checked) face[0] = 1; if (rbHenry_01.Checked) face[0] = 2; if (rbJoker_01.Checked) face[0] = 3; if (rbMona_01.Checked) face[0] = 4; if (rbMonroe_01.Checked) face[0] = 5; if (rbRubens_01.Checked) face[0] = 6; if (rbVanGogh_01.Checked) face[0] = 7; if (rbChurchill_02.Checked) face[1] = 0; if (rbEastwood_02.Checked) face[1] = 1; if (rbHenry_02.Checked) face[1] = 2; if (rbJoker_02.Checked) face[1] = 3; if (rbMona_02.Checked) face[1] = 4; if (rbMonroe_02.Checked) face[1] = 5; if (rbRubens_02.Checked) face[1] = 6; if (rbVanGogh_02.Checked) face[1] = 7; if (rbChurchill_03.Checked) face[2] = 0; if (rbEastwood_03.Checked) face[2] = 1; if (rbHenry_03.Checked) face[2] = 2; if (rbJoker_03.Checked) face[2] = 3; if (rbMona_03.Checked) face[2] = 4; if (rbMonroe_03.Checked) face[2] = 5; if (rbRubens_03.Checked) face[2] = 6; if (rbVanGogh_03.Checked) face[2] = 7; if (rbChurchill_04.Checked) face[3] = 0; if (rbEastwood_04.Checked) face[3] = 1; if (rbHenry_04.Checked) face[3] = 2; if (rbJoker_04.Checked) face[3] = 3; if (rbMona_04.Checked) face[3] = 4; if (rbMonroe_04.Checked) face[3] = 5; if (rbRubens_04.Checked) face[3] = 6; if (rbVanGogh_04.Checked) face[3] = 7; show_face(); } There is a better way to deal with large numbers of Widget properties... for example, How would you create an array of radioButtons?
GDI+ Drawing Demo privatevoid PaintThis() { Graphics g = pictureBox1.CreateGraphics(); Pen MyPen = newPen(Color.Blue); Point a = newPoint(250, 50); Point b = newPoint(50, 250); g.DrawLine(MyPen, a, b); } protectedoverridevoid OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; Pen MyPen = newPen(Color.Red); Point a = newPoint(50, 50); Point b = newPoint(250, 250); g.DrawLine(MyPen, a, b); }