A control that displays a list of items that the user can select from
ListBox
Has various methods and properties that you can use in code to manipulate the ListBox's contents
The Items.Add method allows you to add an item to the ListBox control
The Items.Clear method can erase all the items in the Items property
The Count property reports the number of items stored in the ListBox
Sample Codes
You can add string literals
You can add values of other types as well
while loop
Causes a statement or set of statements to repeat as long as a Boolean expression is true
Structure of a while Loop
1. Boolean expression that is tested for a true or false value
2. Statement or set of statements that is repeated a long as the Boolean expression is true
while Loop
It is a pretest loop, testing its condition before performing an iteration
Requires a counter variable with initial value to be declared, so the Boolean expression can be tested
Sample Code (1 of 4)
The counter has an initial value of 1
Each time the loop executes, 1 is added to counter
The Boolean expression will test whether counter is less than or equal 5. So the loop will stop when counter equals 5
Infinite Loop
A loop that will repeat until the program is interrupted, typically caused by the programmer forgetting to write code that makes the test condition false
Increment
To increase the value of a variable
Decrement
To decrease the value of a variable
Postfix Mode
Placing the ++ and -- operators after their operands
Prefix Mode
Placing the ++ and -- operators before their operands
for Loop
A loop that is specially designed for situations requiring a counter variable to control the number of times that a loop iterates
Structure of a for Loop
1. Initialization: a one-time expression that defines the initial value of the counter
2. Test: A Boolean expression to be tested. If true, the loop iterates.
3. Update: increase or decrease the value of the counter
Sample Code (2 of 4)
The initialization expression assigns 1 to the count variable
The expression count <=5 is tested. If true, continue to display the message.
The update expression adds 1 to the count variable
do-while Loop
A posttest loop, which means it performs an iteration before testing its Boolean expression
Sample Code
Will you see the message box?
Text File
Contains data that has been encoded as test using a 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.
StreamWriter
A class for writing data to a text file
StreamReader
A class for reading data from a text file
Writing Data to a File
1. Create a StreamWriter object
2. Use one of the File methods to open the file to write to
3. Use the Write or WriteLine method to write items of data to the file
4. Close the connection
CreateText
A File method that creates a text file, erasing any existing contents
AppendText
A File method that opens a text file for appending, without erasing any existing contents
Reading Data from a File
1. Create a StreamReader object
2. Use the File.OpenText method to open the file to read from
3. Use the Read or ReadLine method to read items of data from the file
4. Close the connection
OpenFileDialog
A control that displays a standard Windows Open dialog box
SaveFileDialog
A control that displays a standard Windows Save As dialog box
Displaying an Open Box
1. Add the OpenFileDialog control to the form
2. Call the ShowDialog method to display the Open dialog box
DialogResult
A value returned by the ShowDialog method indicating which button the user clicked to dismiss the dialog box
Filename
A property of the OpenFileDialog and SaveFileDialog controls that stores the path and filename of the selected file
InitialDirectory
A property of the OpenFileDialog and SaveFileDialog controls that specifies the directory to be initially displayed
Random
A class in the .NET Framework that generates random numbers
Generating Random Numbers
1. Create a Random object
2. Use the Next method to generate a random integer
3. Use the NextDouble method to generate a random floating-point number between 0.0 and 1.0
Load Event
An event that occurs when a form is loaded into memory
ComboBox
A control that combines a TextBox and a ListBox, allowing the user to either type in a value or select one from a list
ComboBox vs ListBox
ComboBox is appropriate when there is a list of suggested choices (but the user can easily add/insert new items)
ListBox is appropriate when you want to limit input to what is on the list