C Sharp (C#) : Benadir University
C Sharp (C#) : Benadir University
Course: C#
Class: Batch13
Chapter Five
– 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 property
employeeListBox.Items.Clear();
• The Count property reports the number of items stored in the ListBox
Sample Codes
• You can add string literals
private void addButton_Click(object sender, EventArgs e)
{
namesListBox.Items.Add("Chris");
namesListBox.Items.Add("Alicia");
}
False
Structure of a while Loop
• In C#, the generic format of a while loop is:
while (BooleanExpression)
{
Statements;
}
int count = 1;
• Inside the curly braces, there must exist a statement that defines increment (or
decrement) of the counter
or
count++;
or
count += 1;
• Subtracting 1 from a variable can be written as:
count = count – 1;
or
count --;
or
count -= 1;
Postfix Mode vs. Prefix Mode
do
{
Boolean True
statement(s); Expression
} while (BooleanExpression);
False
Sample Codes
• Will you see the message box?
int number = 1
do {
MessageBox.Show(number.ToString());
} while (number < 0);
Using System.IO;
Writing Data to a File
• Start with creating a StreamWriter object
StreamWriter outputFile;
• 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.
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
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();
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”);
Reading Data from a File
• Start with creating a StreamReader object
StreamReader inputFile;
• Use the File.OpenText method to open the file from which you will be
reading data
inputFile = FileOpenText(“students.txt”);
• Use the Read or ReadLine method to read items of data from 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.
• Or
while (!inputFile.EndOfStream) { }
5.8 Random Numbers
• The .NET Framework provides the Random class to
generate random numbers.
• To create an object, use:
Random rand = new Random();
• Two commonly used methods to generate random
numbers are:
– Next: randomly create an integer
– NextDouble: randomly create a floating-point number from 0.0 to
1.0
• Examples,
rand.Next();
rand.NextDouble();
Syntax of Random.Next
Method
• Random.Next generates a random number whose value ranges
from zero to 2,147,483,647
• It also allow you to generate a random number whose value ranges
from zero to some other positive number. The syntax is:
Random.Next(max+1);
rand.Next(100);
5.9 The Load Event
• When running an application, the application’s form is loaded into
memory and an event known as Load takes place
• To create a Load event handler, simply double click the form in the
Designer
• An empty Load event handler looks like:
private void Form1_Load(object sender, EventArgs e) { }
• 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!”);
}
That is……………….
Questions?