listBoxes and loops

listbox
A listBox allows a user to manipulate lists of items.
A user can choose items, add items, delete items and sort items in the list.
We often use loops to manipulate the items in the list.


. about the listBox (lst)
SEE: listBox ref "Items" collection: =================== collection is a group of individual objects treated as one unit "Items" is a collection = the actual list listBox1.Items Add() method: ----------- adds an item to the list boxes Items collection listBox1.Items.Add( "new item" ); Clear() method: ------------ to clear all the items in the listBox listBox1.Items.Clear( ); Properties ========== SelectedItem property: --------------------- value of the selected item in the list if nothing is selected, it contains empty string label1.Text = listBox1.SelectedItem; Text Property:( SAME AS ABOVE ) ------------- value of the selected item in the list if nothing is selected, it contains empty string label1.Text = listBox1.Text; SelectedIndex property: ----------------------- INDEX (int) of the selected item in the list if nothing is selected, it contains -1 if( listBox1.SelectedIndex != -1 ) SelectionMode property: ---------------------- controls the number of choices a user can select None: user can scroll but not select anything One: user can select one item MultiSimple: user can select multiple items contiguous MultiExtended: user can select multiple items non-contiguous Sorted property (t/f) --------------- if true, all items in the list are sorted alpabetically as they are added MultiColumn property (t/f) -------------------- if true, list supports multiple columns

. adding items to listBoxes
User can choose items, add items, delete items and sort items in the list. .
private void btnAdd_Click(object sender, EventArgs e) { // Button1 - add depending what is checked string fruit = "apples"; if( radioButton1.Checked == true ) listBox1.Items.Add( fruit ); // variable else if( radioButton2.Checked ) listBox1.Items.Add( "peaches" ); // literal if( checkBox1.Checked == true ) listBox1.Items.Clear( ); if( checkBox2.Checked ) listBox1.Items.Remove( fruit ); } // add whatever is in text box private void btnAdd_Click(object sender, EventArgs e) { listBox1.Items.Add( textBox1.Text ); }
Can format output using \t and the ToString formatters such as "P", "N" and "C" as well as decimal specifiers like "N4". lstOut.Items.Add( "Ford Falcon \t\t" + price.ToString( "C" ) );

. formatting output to listBoxes
// format {0 is the first item -12 means left justified in a field of 12 // format {1 is the next item, 6 is field width right justified & :C is currency lstOrder.Items.Add(String.Format("{0,-12} : {1,6:C}", "pickle pop", PP));
i dunno
Can format output using \t and the ToString formatters such as "P", "N" and "C" as well as decimal specifiers like "N4". lstOut.Items.Add( "Ford Falcon \t\t" + price.ToString( "C" ) );


. showing, selecting and removing items in listBoxes: ARRAYS
The "Items" collection is an array. An array is indexed, starting at zero. The number of items in the list is "listBox1.Items.Count" MessageBox.Show( "Count of items: " + lstOut.Items.Count.ToString() );
// show only the FIRST item if there are any private void button3_Click(object sender, EventArgs e) { if( listBox1.Items.Count > 0 ) MessageBox.Show( listBox1.Items[0].ToString() ); else MessageBox.Show( "No items in list" ); } // show ALL the items in the list private void button3_Click(object sender, EventArgs e) { if( listBox1.Items.Count > 0 ) { for( int i=0; i < listBox1.Items.Count; i++ ) MessageBox.Show( listBox1.Items[i].ToString() ); } else MessageBox.Show( "No items in list" ); }
// show only the SELECTED item private void button3_Click(object sender, EventArgs e) { try { // careful: selected index is -1 and crashes if nothing selected if( lstOut.SelectedIndex != -1 ) MessageBox.Show( lstOut.Text ); // OR if( lstOut.SelectedIndex != -1 ) MessageBox.Show( lstOut.Items[lstOut.SelectedIndex].ToString() ); } catch( Exception ex ) { MessageBox.Show( "An error occured: " + ex.Message ); } }
// remove the selected item private void button4_Click(object sender, EventArgs e) { // careful: selected index is -1 and crashes if nothing selected if( lstOut.SelectedIndex != -1 ) lstOut.Items.Remove( lstOut.Text ); // OR if( lstOut.SelectedIndex != -1 ) lstOut.Items.Remove( lstOut.Items[lstOut.SelectedIndex] ); }

. looping through the list: ARRAYS
FOR: ==== private void btnPrint_Click(object sender, EventArgs e) { // show all items in list for ( int i = 0; i < listBox1.Items.Count; i++ ) { MessageBox.Show(listBox1.Items[i].ToString()); } // show all selected items in list for ( int i = 0; i < listBox1.SelectedItems.Count; i++ ) { MessageBox.Show( listBox1.SelectedItems[i].ToString() ); } }
DO WHILE: ========= private void btnPrint_Click(object sender, EventArgs e) { int i=0; try { // show all items in list do { // danger: what if NO items in list? MessageBox.Show( listBox1.Items[i].ToString() ); i++; } while( i < listBox1.Items.Count ); } catch( Exception ex ) { MessageBox.Show( "An error occured: " + ex.Message ); } }
WHILE: ====== private void btnPrint_Click(object sender, EventArgs e) { int i=0; try { // show all SELECTED items in list while( i < listBox1.SelectedItems.Count ) { MessageBox.Show(listBox1.SelectedItems[i].ToString()); i++; } } catch( Exception ex ) { MessageBox.Show( "An error occured: " + ex.Message ); } }