ohbutt69

listbox main form

Dec 7th, 2011
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10.  
  11. namespace ListBox
  12. {
  13.     public partial class frmListBox : Form
  14.     {
  15.         public frmListBox()
  16.         {
  17.             InitializeComponent();
  18.  
  19.         }
  20.  
  21.         private void btnAddItems_Click(object sender, EventArgs e)//adds items to the first listbox
  22.         {
  23.             if (rbtnUnseperated.Checked)// if unseperated mode is selected...
  24.             {
  25.                 lbxFirst.Items.Add(txtEntry.Text);// ...add the text as it is
  26.             }
  27.             else if (rbtnSpaceSeperated.Checked)// if space seperated mode is selected...
  28.             {
  29.                 string[] ArraySplitter = txtEntry.Text.Split(' ');// ...pass the text box data into a string seperated by each occurrence of a space...
  30.    
  31.                 foreach (string arraySplits in  ArraySplitter)// ...and for each string in the array...
  32.                 {
  33.                     lbxFirst.Items.Add(arraySplits);// ...and add it to the first list box as a seperate item
  34.                 }
  35.             }
  36.             else if (rbtnCommaSeperated.Checked)// if comma seperated mode is selected...
  37.             {
  38.                 string[] ArraySplitter = txtEntry.Text.Split(',');// ...pass the text box data into a string seperated by each occurrence of a comma...
  39.  
  40.                 foreach (string arraySplits in ArraySplitter)// ...and for each string in the array...
  41.                 {
  42.                     lbxFirst.Items.Add(arraySplits);// ...and add it to the first list box as a seperate item
  43.                 }
  44.             }
  45.             btnReshuffle.Enabled = true;//enable the reshuffle button
  46.             btnRotateDown.Enabled = true;//enable the rotate button
  47.         }
  48.  
  49.         private void btnMoveUp_Click(object sender, EventArgs e)// moves single object up
  50.         {
  51.             if (lbxFirst.SelectedIndex > 0)// if you have selected an item in the list box other than the top item:
  52.             {
  53.                 object itemMoveUp = lbxFirst.SelectedItem;// declare the selected item as an object
  54.                 int index = lbxFirst.SelectedIndex;// declare the selected index as an integer
  55.                 lbxFirst.Items.RemoveAt(index);// remove the item at the index variable (the selected item)
  56.                 lbxFirst.Items.Insert(index -1, itemMoveUp);// inserts the selected item into the list listbox at the position above its previous position
  57.                 lbxFirst.SelectedItem = itemMoveUp;// reselects the item
  58.             }
  59.         }
  60.  
  61.         private void btnRotateDown_Click(object sender, EventArgs e)// rotates items down
  62.         {
  63.                 int index = lbxFirst.Items.Count - 1;// set the index variable to the number of items in the list box minus 1
  64.                 object itemRotate = lbxFirst.Items[index];// sets the itemRotate to the last object in the listbox
  65.                 lbxFirst.Items.RemoveAt(index);// removes the item at the specified index
  66.                 lbxFirst.Items.Insert(0, itemRotate);// inserts the item that was previously at the end of the list box at the first index
  67.         }
  68.  
  69.         private void btnReshuffle_Click(object sender, EventArgs e)// randomly shuffles the items
  70.         {
  71.             Random rand = new Random();// declares a new random as "rand"
  72.             for (int i = lbxFirst.Items.Count - 1; i > 0; i--)// declares the i variable as the number of items -1, loops while i is greater than 0, decrementing 1 each time
  73.             {
  74.                 int next = rand.Next(i);// returns a random positive number as the variable "next" that is less than the value of i
  75.                 object temp = lbxFirst.Items[i];// declares an object "temp" as the item in the listbox at the index of i
  76.                 lbxFirst.Items[i] = lbxFirst.Items[next];// replaces the item at the index of i with the item at the index of the next value
  77.                 lbxFirst.Items[next] = temp;// sets the item at the index of the next value to the object held in the temp variable
  78.             }
  79.         }
  80.  
  81.         private void btnMoveSelectedItem_Click(object sender, EventArgs e)// moves the selected items from the first listbox to the second listbox
  82.         {
  83.             for (int i = lbxFirst.SelectedItems.Count - 1; i >= 0; i--)// sets "i" to the number of selected items in the first listbox - 1, and decrements by 1 until it is equal to 0.
  84.             {
  85.                 lbxSecond.Items.Add(lbxFirst.SelectedItems[i]);// adds the selected item at index i in listbox 1 into listbox 2.
  86.                 lbxFirst.Items.Remove(lbxFirst.SelectedItems[i]);// Removes the selected item at index i in listbox 1
  87.             }
  88.             btnMoveAllItems.Enabled = true;// enables the move all items button
  89.         }
  90.  
  91.         private void btnMoveAllItems_Click(object sender, EventArgs e)//moves all itemas from the second litbox to the first
  92.         {
  93.             for (int i = lbxSecond.Items.Count - 1; i >= 0; i--)// while i (the number of items in listbox 2 - 1) is less more than or equal to 0, decerement i by 1.
  94.             {
  95.                 lbxFirst.Items.Add(lbxSecond.Items[i]);// add the item that are at the index of i in listbox 2 into listbox 1
  96.             }
  97.             lbxSecond.Items.Clear();// removes all items in the second listbox
  98.             btnMoveAllItems.Enabled = false;// disables the move all items button
  99.         }
  100.  
  101.         private void btnClearSelection_Click(object sender, EventArgs e)//clears the selected items in the second listbox
  102.         {
  103.             lbxFirst.ClearSelected();// deselects all selected items in listbox 2
  104.             btnClearSelection.Enabled = false;// sets the clear selected button to false
  105.         }
  106.  
  107.         private void btnDeleteSelected_Click(object sender, EventArgs e)// deletes selected items from the second listbox
  108.         {
  109.             for (int i = lbxSecond.SelectedItems.Count - 1; i >= 0; i--)// sets i to the number of selected items - 1 in listbox 2, and runs until i is = to 0, decrementing i each time
  110.             {
  111.                 lbxSecond.Items.Remove(lbxSecond.SelectedItems[i]);// removes the selected item at the index of i
  112.             }
  113.             if (lbxSecond.Items.Count == 0)// if there is nothing in the second listbox...
  114.             {
  115.                 btnMoveAllItems.Enabled = false;// ...disable the move all items button
  116.             }
  117.         }
  118.  
  119.         private void btnReset_Click(object sender, EventArgs e)//resets the form
  120.         {
  121.             // clears all text and list boxes, and sets buttons to their appropriate states.
  122.             txtEntry.Clear();
  123.             lbxFirst.Items.Clear();
  124.             lbxSecond.Items.Clear();
  125.             btnAddItems.Enabled = false;
  126.             btnMoveUp.Enabled = false;
  127.             btnRotateDown.Enabled = false;
  128.             btnReshuffle.Enabled = false;
  129.             btnClearSelection.Enabled = false;
  130.             btnMoveSelectedItem.Enabled = false;
  131.             btnMoveAllItems.Enabled = false;
  132.             btnDeleteSelected.Enabled = false;
  133.             // clears all text and list boxes, and sets buttons to their appropriate states.
  134.         }
  135.  
  136.         private void btnExit_Click(object sender, EventArgs e)// writes the list of itms in the listbox to a text file and closes the program
  137.         {
  138.             if (MessageBox.Show("Are you sure you want to quit?", "Closing", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)// If the result of the message box is "yes"
  139.             {
  140.                 StreamWriter sW = File.CreateText(@"listtext.txt");// using the StreamWriter, create a file called listtext.txt. this will be save in the same folder as the program executable
  141.                 for (int i = 0; i < lbxFirst.Items.Count; i++)// for runs while "i" is less than the number of items in the first listbox, incrementing i each time.
  142.                 {
  143.                     sW.WriteLine(Convert.ToString(lbxFirst.Items[i]));// converts the item at location i until the conditions of the for loop are met
  144.                 }
  145.                 sW.Close();// closes the StreamWriter
  146.                 this.Close();//Closes the Program
  147.             }
  148.         }
  149.  
  150.         private void lbxFirst_SelectedIndexChanged(object sender, EventArgs e)// this event handler fires when the selected item in the first listbox changes
  151.         {
  152.             if (lbxFirst.SelectedItems.Count == 0)// if the selected items are equal to 0
  153.             {
  154.                 btnClearSelection.Enabled = false; // disable the clear selection button
  155.                 btnMoveSelectedItem.Enabled = false;// disable the move selected item button
  156.             }
  157.             else if (lbxFirst.SelectedItems.Count > 0)// else if the number of selected items is greater than 0
  158.             {
  159.                 btnClearSelection.Enabled = true;// enable the clear selection button
  160.                 btnMoveSelectedItem.Enabled = true;// enable the clear move selected item button
  161.                 if (lbxFirst.SelectedItems.Count == 1)// if the number of selected items equals 1
  162.                 {
  163.                     btnMoveSelectedItem.Text = "Move Selected Item >>";// set the text of the button to imply one item is selected
  164.                 }
  165.                 else if (lbxFirst.SelectedItems.Count > 1)// else if the number of selected items is more than 1
  166.                 {
  167.                     btnMoveSelectedItem.Text = "Move Selected Items >>";// set the text of the button to imply several items are selected
  168.                 }
  169.             }
  170.             if (lbxFirst.SelectedItems.Count == 1 && lbxFirst.SelectedIndex != 0)// if the program has an item selected and it is not at index 0...
  171.             {
  172.                 btnMoveUp.Enabled = true;// ...enable the move up button
  173.             }
  174.             else if (lbxFirst.SelectedIndex == 0)// else if the selected item is at index 0...
  175.             {
  176.                 btnMoveUp.Enabled = false;// ...disable the button
  177.             }
  178.         }
  179.  
  180.         private void lbxSecond_SelectedIndexChanged(object sender, EventArgs e)// this event handler fires when the selected item in the second listbox changes
  181.         {
  182.             if (lbxSecond.SelectedItems.Count == 0)// if the number of selected items is 0...
  183.             {
  184.                 btnDeleteSelected.Enabled = false;// ...disable the delete selected button
  185.             }
  186.             else if (lbxSecond.SelectedItems.Count > 0)// else if the number of selected items is greater than 0...
  187.             {
  188.                 btnDeleteSelected.Enabled = true;// ... enable the delete selected button
  189.             }
  190.             if (lbxSecond.Items.Count >= 1)// if the number of items in the second list box is greater than or equal to 1...
  191.             {
  192.                 btnMoveAllItems.Enabled = true;// ...enable the move all items button...
  193.             }
  194.             else// ...but if it isn't...
  195.             {
  196.                 btnMoveAllItems.Enabled = false;// ...disable the move all items button
  197.             }
  198.         }
  199.  
  200.         private void frmListBox_Load(object sender, EventArgs e)// this event handler fires when the form loads, to supply the first list box with saved text
  201.         {
  202.             StreamReader sR = new StreamReader(Convert.ToString(@"listtext.txt"));// opens a new instance of StreamReader, and reads from the file "listtext.txt"
  203.             while (sR.Peek() >= 0)// the peek method supplies the program with a number that says if there is any more text to read, this code runs while the .peek() number is greater than 0
  204.             {
  205.                 lbxFirst.Items.Add(Convert.ToString(sR.ReadLine()));// adds items into the list box as it reads them from the file
  206.             }
  207.             if (lbxFirst.Items.Count >= 1)// if the number of items is greater than or equal to 1..
  208.             {
  209.                 btnReshuffle.Enabled = true;// enable the reshuffle button
  210.                 btnRotateDown.Enabled = true;// enable the rotate down button
  211.             }
  212.             else// but if the number of items are not greater than or equal to 1
  213.             {
  214.                 btnReshuffle.Enabled = false;// disable the reshuffle button
  215.                 btnRotateDown.Enabled = false;// disable the rotate down button
  216.             }
  217.             sR.Close();// close the StreamReader method
  218.         }
  219.  
  220.         private void txtEntry_TextChanged(object sender, EventArgs e)// this event handler fires if the text is changed in the textEntry text box
  221.         {
  222.             if (txtEntry.Text.Length > 0)// if the length of the text is greater than 0...
  223.             {
  224.                 btnAddItems.Enabled = true;// ...enable the add items button
  225.             }
  226.             else if (txtEntry.Text.Length == 0)// else if the length of the text is equal to 0...
  227.             {
  228.                 btnAddItems.Enabled = false;// ...disable the add items button
  229.             }
  230.         }
  231.     }
  232. }// program ends
  233.  
  234.  
Advertisement
Add Comment
Please, Sign In to add comment