Loops, File, and Random Numbers
Loops, File, and Random Numbers
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
5.1 More About ListBoxes
ListBox controls have various methods and properties that you can
use in code to manipulate the ListBoxs contents
The Items.Add method allows you to add an item to the ListBox
control
ListBoxName.Items.Add(Item);
where ListBoxName is the name of the ListBox control; Item is the value
to be added to the Items property
The Items.Clear method can erase all the items in the Items
proeprty
employeeListBox.Items.Clear();
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
5.2 The while Loop
False
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Structure of a while Loop
while (BooleanExpression)
{
Statements;
}
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
The while Loop is a Pretest Loop
int count = 1;
Inside the curly braces, there must exist a statement that defines increment
(or decrement) of the counter
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Sample Code
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Infinite Loops
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
5.3 The ++ and -- Operators
or
count++;
or
count += 1;
or
count --;
or
count -= 1;
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Postfix Mode vs. Prefix Mode
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
5.4 The for Loop
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Sample Code
int count;
// declare count variable in initialization expression
for (count = 1; count <= 5; count++)
for (int count = 1; count <= 5; count++)
{ {
MessageBox.Show(Hello); MessageBox.Show(Hello);
} }
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Other Forms of Update
Expression
In the update expression, the counter variable is typically
incremented by 1. But, this is not a requirement.
//increment by 10
for (int count = 0; count <=100; count += 10)
{
MessageBox.Show(count.ToString());
}
You can decrement the counter variable to make it count backward
//counting backward
for (int count = 10; count >=0; count--)
{
MessageBox.Show(count.ToString());
}
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
5.5 The do-while Loop
} while (BooleanExpression);
False
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Sample Codes
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
5.6 Using File for Data Storage
When a program needs to save data for later use, it writes the data
in a file
There are always three steps:
Open the file: create a connection between the file and the program
Process the file: either write to or read from the file
Close the file: disconnect the file and the program
In general, there are two types of files:
Text file: contains data that has been encoded as test using scheme
such as Unicode
Binary file: contains data that has not been converted to text. You
cannot view the contents of binary files with a text editor.
This chapter only works with text files
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
File Accessing
Using System.IO;
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Writing Data to a File
Use one of the File methods to open the file to which you will be
writing data. Sample File methods are:
File.CreateText
File.AppendText
Use the Write or WriteLine method to write items of data to the file
Close the connection.
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Sample Code
StreamWriter outputFile;
outputFile = File.CreateText(courses.txt);
outputFile.WriteLine(Introduction to Computer Science);
outputFile.WriteLine(English Composition);
outputFile.Write(Calculus I);
outputFile.Close();
The WriteLine method writes an item of data to a file and then writes a
newline characters which specifies the end of a line
The Write method writes an item to a file without a newline character
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
CreateText vs. AppendText
The previous code uses the File.CreateText method for the following
reasons:
It creates a text file with the name specified by the argument. If the file
already exists, its contents are erased
It creates a StreamWriter object in memory, associated with the file
It returns a reference to the StreamWriter object
When there is a need not to erase the contents of an existing file,
use the AppendText method
StreamWriter outputFile;
outputFile = File.AppendText(Names.txt);
outputFile.WriteLine(Lynn);
outputFile.WriteLine(Steve);
outputFile.Close();
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Specifying the Location of an
Output File
If you want to open a file in a different location,
you can specify a path as well as filename in the
argument
Be sure to prefix the string with the @ character
StreamWriter outputFile;
outputFile = File.CreateText(@C:\Users\chris\Documents\Names.txt);
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Reading Data from a File
Use the File.OpenText method to open the file to which you will be
writing data
inputFile = FileOpenText(students.txt);
Use the Read or ReadLine method to write items of data to the file
StreamReader.ReadLine: Reads a line of characters from the current stream and
returns the data as a string.
StreamReader.Read: Reads the next character or next set of characters from the
input stream.
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Reading a File with a Loop
Or
while (!inputFile.EndOfStream) { }
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
5.7 The OpenFileDialog and
SaveFileDialog Controls
The OpenFileDialog and SaveDialog controls allow your
application to display standard Windows dialog boxes for opening
and saving files
Unlike Label, Button, and TextBox, they are invisible controls
The OpenFileDialog control displays a standard Windows Open
dialog box.
The SaveDialog control displays a standard Windows Save As
dialog box
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Displaying an Open Box
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Detecting the Users Selection
if (openFile.ShowDialog() == DialogResult.OK) { }
else if (openFile.ShowDialog() == DialogResult.Cancel) { }
else { }
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
The Filename and InitialDirectory
Property
When the user selects a file with the Open dialog box, the files path
and filename are stored in the controls Filename property
The following is an example of how to open the selected file:
if (openFile.ShowDialog() == DialogResult.OK)
{
inputFile = File.OpenText(openFile.Filename);
}
else { }
You can specify a directory to be initially displayed with the
InitialDirectory property. For example,
openFile.InitialDirectory = C:\Data;
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Displaying a Save As Dialog Box
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
5.8 Random Numbers
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Syntax of Random.Next Method
Random.Next(max+1);
rand.Next(10);
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
5.9 The Load Event
Any code you write inside the Load event will execute when the form
is launched. For example,
private void Form1_Load(object sender, EventArgs e)
{
MessageBox.Show(Prepare to see the form!);
}
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.