Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- namespace ListBox
- {
- public partial class frmListBox : Form
- {
- public frmListBox()
- {
- InitializeComponent();
- }
- private void btnAddItems_Click(object sender, EventArgs e)//adds items to the first listbox
- {
- if (rbtnUnseperated.Checked)// if unseperated mode is selected...
- {
- lbxFirst.Items.Add(txtEntry.Text);// ...add the text as it is
- }
- else if (rbtnSpaceSeperated.Checked)// if space seperated mode is selected...
- {
- string[] ArraySplitter = txtEntry.Text.Split(' ');// ...pass the text box data into a string seperated by each occurrence of a space...
- foreach (string arraySplits in ArraySplitter)// ...and for each string in the array...
- {
- lbxFirst.Items.Add(arraySplits);// ...and add it to the first list box as a seperate item
- }
- }
- else if (rbtnCommaSeperated.Checked)// if comma seperated mode is selected...
- {
- string[] ArraySplitter = txtEntry.Text.Split(',');// ...pass the text box data into a string seperated by each occurrence of a comma...
- foreach (string arraySplits in ArraySplitter)// ...and for each string in the array...
- {
- lbxFirst.Items.Add(arraySplits);// ...and add it to the first list box as a seperate item
- }
- }
- btnReshuffle.Enabled = true;//enable the reshuffle button
- btnRotateDown.Enabled = true;//enable the rotate button
- }
- private void btnMoveUp_Click(object sender, EventArgs e)// moves single object up
- {
- if (lbxFirst.SelectedIndex > 0)// if you have selected an item in the list box other than the top item:
- {
- object itemMoveUp = lbxFirst.SelectedItem;// declare the selected item as an object
- int index = lbxFirst.SelectedIndex;// declare the selected index as an integer
- lbxFirst.Items.RemoveAt(index);// remove the item at the index variable (the selected item)
- lbxFirst.Items.Insert(index -1, itemMoveUp);// inserts the selected item into the list listbox at the position above its previous position
- lbxFirst.SelectedItem = itemMoveUp;// reselects the item
- }
- }
- private void btnRotateDown_Click(object sender, EventArgs e)// rotates items down
- {
- int index = lbxFirst.Items.Count - 1;// set the index variable to the number of items in the list box minus 1
- object itemRotate = lbxFirst.Items[index];// sets the itemRotate to the last object in the listbox
- lbxFirst.Items.RemoveAt(index);// removes the item at the specified index
- lbxFirst.Items.Insert(0, itemRotate);// inserts the item that was previously at the end of the list box at the first index
- }
- private void btnReshuffle_Click(object sender, EventArgs e)// randomly shuffles the items
- {
- Random rand = new Random();// declares a new random as "rand"
- 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
- {
- int next = rand.Next(i);// returns a random positive number as the variable "next" that is less than the value of i
- object temp = lbxFirst.Items[i];// declares an object "temp" as the item in the listbox at the index of i
- lbxFirst.Items[i] = lbxFirst.Items[next];// replaces the item at the index of i with the item at the index of the next value
- lbxFirst.Items[next] = temp;// sets the item at the index of the next value to the object held in the temp variable
- }
- }
- private void btnMoveSelectedItem_Click(object sender, EventArgs e)// moves the selected items from the first listbox to the second listbox
- {
- 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.
- {
- lbxSecond.Items.Add(lbxFirst.SelectedItems[i]);// adds the selected item at index i in listbox 1 into listbox 2.
- lbxFirst.Items.Remove(lbxFirst.SelectedItems[i]);// Removes the selected item at index i in listbox 1
- }
- btnMoveAllItems.Enabled = true;// enables the move all items button
- }
- private void btnMoveAllItems_Click(object sender, EventArgs e)//moves all itemas from the second litbox to the first
- {
- 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.
- {
- lbxFirst.Items.Add(lbxSecond.Items[i]);// add the item that are at the index of i in listbox 2 into listbox 1
- }
- lbxSecond.Items.Clear();// removes all items in the second listbox
- btnMoveAllItems.Enabled = false;// disables the move all items button
- }
- private void btnClearSelection_Click(object sender, EventArgs e)//clears the selected items in the second listbox
- {
- lbxFirst.ClearSelected();// deselects all selected items in listbox 2
- btnClearSelection.Enabled = false;// sets the clear selected button to false
- }
- private void btnDeleteSelected_Click(object sender, EventArgs e)// deletes selected items from the second listbox
- {
- 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
- {
- lbxSecond.Items.Remove(lbxSecond.SelectedItems[i]);// removes the selected item at the index of i
- }
- if (lbxSecond.Items.Count == 0)// if there is nothing in the second listbox...
- {
- btnMoveAllItems.Enabled = false;// ...disable the move all items button
- }
- }
- private void btnReset_Click(object sender, EventArgs e)//resets the form
- {
- // clears all text and list boxes, and sets buttons to their appropriate states.
- txtEntry.Clear();
- lbxFirst.Items.Clear();
- lbxSecond.Items.Clear();
- btnAddItems.Enabled = false;
- btnMoveUp.Enabled = false;
- btnRotateDown.Enabled = false;
- btnReshuffle.Enabled = false;
- btnClearSelection.Enabled = false;
- btnMoveSelectedItem.Enabled = false;
- btnMoveAllItems.Enabled = false;
- btnDeleteSelected.Enabled = false;
- // clears all text and list boxes, and sets buttons to their appropriate states.
- }
- private void btnExit_Click(object sender, EventArgs e)// writes the list of itms in the listbox to a text file and closes the program
- {
- 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"
- {
- 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
- 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.
- {
- sW.WriteLine(Convert.ToString(lbxFirst.Items[i]));// converts the item at location i until the conditions of the for loop are met
- }
- sW.Close();// closes the StreamWriter
- this.Close();//Closes the Program
- }
- }
- private void lbxFirst_SelectedIndexChanged(object sender, EventArgs e)// this event handler fires when the selected item in the first listbox changes
- {
- if (lbxFirst.SelectedItems.Count == 0)// if the selected items are equal to 0
- {
- btnClearSelection.Enabled = false; // disable the clear selection button
- btnMoveSelectedItem.Enabled = false;// disable the move selected item button
- }
- else if (lbxFirst.SelectedItems.Count > 0)// else if the number of selected items is greater than 0
- {
- btnClearSelection.Enabled = true;// enable the clear selection button
- btnMoveSelectedItem.Enabled = true;// enable the clear move selected item button
- if (lbxFirst.SelectedItems.Count == 1)// if the number of selected items equals 1
- {
- btnMoveSelectedItem.Text = "Move Selected Item >>";// set the text of the button to imply one item is selected
- }
- else if (lbxFirst.SelectedItems.Count > 1)// else if the number of selected items is more than 1
- {
- btnMoveSelectedItem.Text = "Move Selected Items >>";// set the text of the button to imply several items are selected
- }
- }
- if (lbxFirst.SelectedItems.Count == 1 && lbxFirst.SelectedIndex != 0)// if the program has an item selected and it is not at index 0...
- {
- btnMoveUp.Enabled = true;// ...enable the move up button
- }
- else if (lbxFirst.SelectedIndex == 0)// else if the selected item is at index 0...
- {
- btnMoveUp.Enabled = false;// ...disable the button
- }
- }
- private void lbxSecond_SelectedIndexChanged(object sender, EventArgs e)// this event handler fires when the selected item in the second listbox changes
- {
- if (lbxSecond.SelectedItems.Count == 0)// if the number of selected items is 0...
- {
- btnDeleteSelected.Enabled = false;// ...disable the delete selected button
- }
- else if (lbxSecond.SelectedItems.Count > 0)// else if the number of selected items is greater than 0...
- {
- btnDeleteSelected.Enabled = true;// ... enable the delete selected button
- }
- if (lbxSecond.Items.Count >= 1)// if the number of items in the second list box is greater than or equal to 1...
- {
- btnMoveAllItems.Enabled = true;// ...enable the move all items button...
- }
- else// ...but if it isn't...
- {
- btnMoveAllItems.Enabled = false;// ...disable the move all items button
- }
- }
- 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
- {
- StreamReader sR = new StreamReader(Convert.ToString(@"listtext.txt"));// opens a new instance of StreamReader, and reads from the file "listtext.txt"
- 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
- {
- lbxFirst.Items.Add(Convert.ToString(sR.ReadLine()));// adds items into the list box as it reads them from the file
- }
- if (lbxFirst.Items.Count >= 1)// if the number of items is greater than or equal to 1..
- {
- btnReshuffle.Enabled = true;// enable the reshuffle button
- btnRotateDown.Enabled = true;// enable the rotate down button
- }
- else// but if the number of items are not greater than or equal to 1
- {
- btnReshuffle.Enabled = false;// disable the reshuffle button
- btnRotateDown.Enabled = false;// disable the rotate down button
- }
- sR.Close();// close the StreamReader method
- }
- private void txtEntry_TextChanged(object sender, EventArgs e)// this event handler fires if the text is changed in the textEntry text box
- {
- if (txtEntry.Text.Length > 0)// if the length of the text is greater than 0...
- {
- btnAddItems.Enabled = true;// ...enable the add items button
- }
- else if (txtEntry.Text.Length == 0)// else if the length of the text is equal to 0...
- {
- btnAddItems.Enabled = false;// ...disable the add items button
- }
- }
- }
- }// program ends
Advertisement
Add Comment
Please, Sign In to add comment