5a: LISTBOXES, LOOPS & RANDOM NUMBERS

5.1 More About ListBoxes 5.2 The while Loop 5.3 The ++ and -- Operators 5.4 The for Loop 5.5 The do-while Loop 5.6 Files for Storage 5.7 OpenFile Dialog & SaveFile Dialog 5.8 Random Numbers 5.9 Load Event 5.x Tabs & Timers Console: loops

5.1 MORE ABOUT 'ListBox'

ListBox controls have methods and properties to manipulate the ListBox's contents Items.Add method allows you to add an item to the ListBox control lstName.Items.Add(Item); Items.Clear method can erase all the items in the Items proeprty lstEmployee.Items.Clear(); Count property reports the number of items stored in the ListBox int num = lstNames.Items.Count; About listBoxes & loops
      private void btnAdd_Click(object sender, EventArgs e) 
      {
         namesListBox.Items.Add("Chris");
         namesListBox.Items.Add("Alicia");
         namesListBox.Items.Add(10);
         namesListBox.Items.Add(17.5);
      }       
 
      
Assignment & Arithmetic (textBox, listBox, parse, append)  

5.2 THE 'while' LOOP

The while loop causes a statement or set of statements to repeat as long as a Boolean expression is true A while loop has two parts: A Boolean expression that is tested for a true or false value A statement or set of statements that is repeated a long as the Boolean expression is true while (BooleanExpression) // while clause { Statements; // body } The while Loop is a Pretest Loop - it tests its condition before performing an iteration. Inside the curly braces, there must exist a statement that defines increment (or decrement) of the counter while (count < 5) { ... counter = count + 1; }
      private void goButton_Click(object sender, EventArgs e)
      {
        int count = 1;
        while (count <= 5)
        {
           MessageBox.Show("Hello!");
           count = count + 1;
        }
      }  
      
      
About: do while, while, for loops Infinite Loops are BAD int count = 1; while (count <= 5) { MessageBox.Show("Hello"); } Ending Balance (loop)   A ListBox can capture ALL the output within a loop. Ending Balance (loop, listBox)  

5.3 THE '++' & '--' OPERATORS

To increment a variable means to increase its value, and to decrement a variable means to decrease its value Adding 1 to a variable can be written as:
         count = count + 1;
          // 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 Postfix mode means to place the ++ and -- operators after their operands
         count++;  
         
         
Prefix mode means to place the ++ and -- operators before their operands
         --count;  
         
         

5.4 THE 'for' LOOP

The for loop is designed for situations requiring a counter variable to control the number of times that a loop iterates You must specify three actions: Initialization: a one-time expression that defines the initial value of the counter Test: A Boolean expression to be tested. If true, the loop iterates. Update: increase or decrease the value of the counter The for loop is a pretest loop
         int count;
         for (count = 1; count <= 5; count++)
         {
            MessageBox.Show("Hello");
         }  
         
         
The initialization expression assign 1 to the count variable The expression count <=5 is tested. If true, continue to display the message. The update expression add 1 to the count variable Start the loop over Squares to a Listbox Other Forms of Update Expression
      //increment by 10
      for (int count = 0; count <=100; count += 10)
      {
         MessageBox.Show(count.ToString());
      }

      //counting backward
      for (int count = 10; count >=0; count--)
      {
         MessageBox.Show(count.ToString());
      } 
 
      
Speed Conversion  

5.5 THE 'do-while' LOOP

The do-while loop is a posttest loop: it performs an iteration before testing its Boolean expression. Will you see the message box?
         int number = 1;
         do {
             MessageBox.Show(number.ToString());
         } while (number < 0);  
         
         
Will you see the message box?
         int number = 1;
         while (number < 0)
         {
            MessageBox.Show(number.ToString());
         }  
         
         
About: do while, while, for loops Loop code

5.6 FILES for STORAGE

About Files Read file in a loop & output to ListBox Read file on form load Read file in loop and accumulate sales

5.7 DIALOGS

About Dialogs, Colors & Hex

5.8 RANDOM NUMBERS

The .NET Framework provides the Random class to generate random numbers. To create an object, use: Random rand = new Random(); About: random numbers, classes & objects 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
         rand.Next();
      
         // to create a random number from 0 to 99, use:
         rand.Next(100);  
         
         // to create a random number from 1 to 10, use:
         rand.Next(1, 11);  
         
         
Coin Toss (random #, if, visible)   Guessing Game (random #, if, timer)  

5.9 THE 'Load' EVENT

To create a Load event handler, simply double click the form in the Designer Any code you write inside the Load event will execute when the form is launched. Only executes ONE time... For example,
      private void Form1_Load(object sender, EventArgs e)
      {
        MessageBox.Show("Prepare to see the form!");
      }  
      
      

5.x TABS & TIMERS

About: TabControls and Timers