Lecture 14: File I/O Common Dialogs Toolbox Widgets.

Slides:



Advertisements
Similar presentations
 2007 Dr. Natheer Khasawneh. Chapter 13. Graphical User Interface Concepts: Part 1.
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 11 MORE WINDOWS CONTROLS & STANDARD DIALOG BOXES.
C# - Files and Streams Outline Files and Streams Classes File and Directory Creating a Sequential-Access File Reading Data from a Sequential-Access.
File Access, Dialog Boxes, Error Handling, and Menus Chapter 5.
Chapter 12- GUI Concepts I. 12.1Introduction 12.2 Windows Forms 12.3 Event-Handling Model Basic Event Handling 12.4 Control Properties and Layout.
.NET Class 4 – Windows-based Application. WinForm Application Homogeny programming model. Rich class library Classes are shared by all.NET languages.
Copyright © 2012 Pearson Education, Inc. Chapter 2 Introduction to Visual C#
DT265-2 Object Oriented Software Development 2 Lecture 3 : Windows Programming Lecturer Pat Browne
1 Windows Printing. 2 Objectives You will be able to Output text and graphics to a printer. Print multipage documents. Use the standard Windows print.
1 Graphical User Interfaces Part 2 Outline Multiple Document Interface (MDI) Windows Visual Inheritance User-Defined Controls.
ADO.NET By Hanumantha Rao.N MCA By Hanumantha Rao.N MCA.
Dr Dat Tran - Week 4 Lecture Notes 1 MenuStrip Programming Graphical User Interfaces PG (7110) University of Canberra School of Information Sciences &
File Handling. Data Files Programs that can only get data from the user interface are limited. –data must be entered each time. –only small amounts of.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Object Oriented Programming Graphical User Interfaces Dr. Mike Spann
CSCI 3328 Object Oriented Programming in C# Chapter 2: Introduction to Visual C# Programming 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg,
Lecture 8 Building an MDI Application. Introduction The MDI (Multiple Document Interface) provides a way to display multiple (child) windows forms inside.
Object Oriented Programming Graphics and Multimedia Dr. Mike Spann
C# Programming Lecture 4 “GDI+” PGL01/CSP/2006.
Copyright © 2006 Thomas P. Skinner1 Chapter 5 Indexers, Interfaces, and Enumerators.
Animation and Double-Buffering. The animation methods described here are based on standard techniques of double-buffering applicable to most high-level.
1 Working with Menus and Dialog Boxes. 2 Objectives You will be able to Create and edit menus for Windows Forms applications. Write code to handle menu.
Dr Dat Tran - Week 1 Lecture Notes 1 Windows Forms Programming Graphical User Interfaces PG (7110) University of Canberra School of Information Sciences.
Dr Dat Tran - Week 4 Lecture Notes 1 ToolStrip Programming Graphical User Interfaces PG (7110) University of Canberra School of Information Sciences &
CST238 Week 4 Questions / Concerns? Announcements – Start thinking about project ideas – Test#1 next Monday, 4/28/14 Recap Check-off Take Home Lab#3 New.
File I/O 11_file_processing.ppt
Chapter 11 Bitmaps, Icons and Cursors. Copyright 2006 © Thomas P. Skinner2 Background Bitmaps are easy in.NET. Two kinds of graphics: 1.Vector 2.Bitmaps.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
11-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Lecture 16: Multithreaded Programming. public partial class Form1 : Form { Thread ct; Thread rt; public static int circle_sleep = 0; public static int.
BIL528 – Bilgisayar Programlama II Introduction 1.
Lecture Set 12 Sequential Files and Structures Part A – Dialog Boxes, Filters, Directories.
Dynamic Dropdown Lists 1. Objectives You will be able to Use Dropdown Lists to solicit multiple choice user input in an ASPX web page. Populate a Dropdown.
BIM211 – Visual Programming Interacting with Users Graphics 1.
G RAPHICAL U SER I NTERFACE C ONCEPTS : P ART 2 1 Outline Control Properties and Layout Label s, TextBox es and Button s GroupBox es and Panel s CheckBox.
Lecture 6: Advanced GUI Controls. Up to now we have used simple controls such as buttons and textboxes. Now we will review some interactive (e.g. dialog)
Lecture 7 Dialog Controls MDI Parent/Child Interaction.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Developing Windows Applications with Visual Studio Windows Forms Controls and Coding Jim Warren – COMPSCI 280 S Enterprise.
C# Data type Chapter 1.
Dr Dat Tran - Week 2 Lecture Notes 1 Forms Programming Graphical User Interfaces PG (7110) University of Canberra School of Information Sciences & Engineering.
Copyright © 2012 Pearson Education, Inc. Chapter 5 Loops, File, and Random Numbers.
CSCI 3328 Object Oriented Programming in C# Chapter 11: Files and Streams -- Exercises 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg,
Building Your Own Class. We will build a simple instance class for an inventory control application and a main program to test it. We need an Item class.
1 Printing a Document. 2 Objectives You will be able to print a real multipage document.
Error Trapping Exception Catching 1. Syntax & Compile-time  VB – Editor/Compiler Logic and Design  You Runtime  You Types of Errors: 2 When your program.
Graphical User Interface Components Version 1.1. Obectives Students should understand how to use these basic Form components to create a Graphical User.
1 Windows Forms II Chapter RadioButton / GroupBox Controls Used to solicit a multiple choice input. Radio buttons work as a group. Selecting one.
McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 11 Data Files.
Lecture 11: Generics. The Generic List loading data from a file using System.IO; : namespace EmpListDemo { static class Program { static void Main(string[]
Windows form programming. namespace MyNamespace { public class MyForm : System.Windows.Forms.Form { public MyForm() { this.Text = "Hello Form"; }
Module 3: Creating Windows-based Applications. Overview Creating the Main Menu Creating and Using Common Dialog Boxes Creating and Using Custom Dialog.
PictureBox, MessageBox, Multiple Forms, Splash Screens and Timers
Computing with C# and the .NET Framework
CSCI 3328 Object Oriented Programming in C# Chapter 2: Introduction to Visual C# Programming UTPA – Fall 2012 This set of slides is revised from lecture.
Object Oriented Programming
Building Your Own Class
CSCI 3327 Visual Basic Chapter 11: Files and Streams
How to Add Images Using an 'openFile' Dialog
MDI Picture Viewer Application
ThS. Nguyễn Hà Giang Khoa CNTT - Hutech
The University of Texas – Pan American
Copyright © 2006 Thomas P. Skinner
The University of Texas – Pan American
CSCI 3328 Object Oriented Programming in C# Chapter 11: Files and Streams -- Exercises UTPA – Fall 2012 This set of slides is revised from lecture slides.
Structured Variables & File Systems
Module 8: Creating Windows-based Applications
Based on Murach Chapter 10
Creating Controls Dynamically in C#
Presentation transcript:

Lecture 14: File I/O Common Dialogs Toolbox Widgets

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. Introduction

array.txt Reading a Textfile In the example code ReadTextArray a 10x10 array of integers named array.txt is read and loaded into a two-dimensional array.

string fname = ""; string textline = ""; fname = txtFilename.Text; int j; try { TextReader tr = new StreamReader(fname); n = Convert.ToInt32(tr.ReadLine()); txtDisplay.Text = Convert.ToString(n) + "\r\n"; W = new int[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"; } Reading an Array of Integers

private void 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; } Using the Array

array.txt The Benefit of Checking for Empty String

using System.IO; : private void OnFileOpen(object sender, EventArgs e) { OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "text files (*.txt)|*.txt"; dialog.ShowDialog(); filename = dialog.FileName; OpenFile(); } private void OnFileSaveAs(object sender, EventArgs e) { if (dlgSaveFile.ShowDialog() == DialogResult.OK) { filename = dlgSaveFile.FileName; SaveFile(); } The Purpose of Common File Dialogs: to retreive a path and filename

try { printDocument.Print(); } catch (InvalidPrinterException ex) { MessageBox.Show(ex.Message, "Print Ticket", MessageBoxButtons.OK, MessageBoxIcon.Error); } : private void printDocument_PrintPage(object sender, PrintPageEventArgs e) { Bitmap center = new Bitmap("ticket_header_center.jpg"); string line = ""; line = txtFirst.Text + " " + txtLast.Text; e.Graphics.DrawString(line, new Font("Arial", 12),Brushes.Black, 20, 240); line = txtStreet.Text; e.Graphics.DrawString(line, new Font("Arial", 12), Brushes.Black, 20, 260); line = txtCity.Text + ", " + txtState.Text + " " + txtZip.Text; e.Graphics.DrawString(line, new Font("Arial", 12), Brushes.Black, 20, 280); e.Graphics.DrawImage(center,275, 20); } Printing a Document with Images and Text

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.

public partial class msgBoxChild : Form { private MessageBoxButtons buttonType = MessageBoxButtons.OK; private MessageBoxIcon iconType = MessageBoxIcon.Exclamation; public msgBoxChild(WidgetDemo.widgetMain parent) { InitializeComponent(); this.MdiParent = parent; rbExclamation.Checked = true; rb_OK.Checked = true; } private void 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); } messageBox Demo

Mouse Demo private Point mpt; public mouseChild(WidgetDemo.widgetMain parent) { InitializeComponent(); this.MdiParent = parent; } private void mouseChild_MouseMove(object sender, MouseEventArgs e) { Point lpt = new Point(); 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); } private void mouseChild_MouseDown(object sender, MouseEventArgs e) { MessageBox.Show(Convert.ToString(e.Button) + " button Pressed"); }

private void newToolStripButton_Click(object sender, EventArgs e) { picBox.Image = null; this.Width = 200; this.Height = 200; } private void openToolStripButton_Click(object sender, EventArgs e) { int minWidth = 100; int minHeight = 50; string fileName = ""; OpenFileDialog dlg = new OpenFileDialog(); if (dlg.ShowDialog() == DialogResult.OK) { fileName = dlg.FileName; Bitmap img = new Bitmap(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; } pictureBox Demo

private DateTime dtobj = new DateTime(); public calChild(WidgetDemo.widgetMain parent) { InitializeComponent(); this.MdiParent = parent; } private void 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); } monthCalendar Demo

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { linkLabel1.LinkVisited = true; } private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { linkLabel2.LinkVisited = true; System.Diagnostics.Process.Start("notepad"); } private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { linkLabel3.LinkVisited = true; System.Diagnostics.Process.Start(" } linkLabel Demo

private void listBoxFavs_MouseUp(object sender, MouseEventArgs e) { rtbxSelected.Text = ""; foreach (string item in listBoxFavs.SelectedItems) { rtbxSelected.Text += item + "\n"; }

comboBox Demo

private string[,] countrydata = new string[237, 3]; private void load_country_data() { string textline; int colnum; int rownum = 0; StreamReader tr = new StreamReader("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"; }

private void 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 public partial class tabChild : Form { private int[] face = new int[4] { 0, 0, 0, 0 }; private Bitmap[] head = new Bitmap[8]; private Bitmap[] eyes = new Bitmap[8]; private Bitmap[] nose = new Bitmap[8]; private Bitmap[] chin = new Bitmap[8]; private bool in_color = true;

public void load_images() { string[] person = new string[] {"churchill", "eastwood", "henryVIII", "joker", "mona", "monroe", "rubens", "vangogh"}; string filename; for (int i = 0; i < 8; i++) { filename = person[i] + "_01.jpg"; head[i] = new Bitmap(filename); filename = person[i] + "_02.jpg"; eyes[i] = new Bitmap(filename); filename = person[i] + "_03.jpg"; nose[i] = new Bitmap(filename); filename = person[i] + "_04.jpg"; chin[i] = new Bitmap(filename); }

public void 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 = new Bitmap(head[face[0]]); Grayscale(ref img0); picBoxHead.Image = img0; Bitmap img1 = new Bitmap(eyes[face[1]]); Grayscale(ref img1); picBoxEyes.Image = img1; Bitmap img2 = new Bitmap(nose[face[2]]); Grayscale(ref img2); picBoxNose.Image = img2; Bitmap img3 = new Bitmap(chin[face[3]]); Grayscale(ref img3); picBoxChin.Image = img3; }

private void 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 protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; Pen MyPen = new Pen(Color.Red); Point a = new Point(50, 50); Point b = new Point(250, 250); g.DrawLine(MyPen, a, b); } private void PaintThis() { Graphics g = pictureBox1.CreateGraphics(); Pen MyPen = new Pen(Color.Blue); Point a = new Point(250, 50); Point b = new Point(50, 250); g.DrawLine(MyPen, a, b); }