Friday, May 3, 2013

Select All, Clear All, Cut, Copy, Paste on List Box


Have you ever imagined our life without “Cut-Copy-Paste” in today’s computer era… specially a Software developer’s life! These clipboard operations are very common and most frequently used, but here I am going to discuss how to code them (on a List Box control) instead of just using them.

In this article, I am going to show some edit operations like: Select All, Clear All, Cut, Copy and Paste on a List Box control. To demonstrate this, I created a simple Win form application with a Menu Strip and a List Box control.   On the menu Strip, all the operations are entered as menu items with short cut keys.
For better understanding, sample code is also attached with this article.


Click to download source code

Here is a screenshot of the GUI.


Select All:

To select all the items in a ListBox, we clear all the selected items firstly and keep on selecting item while iterating the entire list.

     private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)  
     {  
       try  
       {  
         listBox1.SelectedItems.Clear();  
         for (int i = 0; i < listBox1.Items.Count; i++)  
         {  
           listBox1.SetSelected(i, true);  
         }  
       }  
       catch (Exception ex)  
       {  
         MessageBox.Show(ex.Message);  
       }  
     }  

Paste:

To paste the text data into our List Box, we firstly retrieve the text data from buffer using Clipboard class. Since it is a list box where each item in the List Box should correspond to each line in the buffer, so here we parse the Text data by a new line character and then store each line as a new item in the List Box.
     private void pasteToolStripMenuItem_Click(object sender, EventArgs e)  
     {  
       try  
       {  
         // Getting Text from Clip board  
         string s = Clipboard.GetText();  
         //Parsing criteria: New Line  
         string[] lines = s.Split('\n');  
         foreach (string ln in lines)  
         {  
           listBox1.Items.Add(ln.Trim());  
         }  
       }  
       catch (Exception ex)  
       {  
         MessageBox.Show(ex.Message);  
       }  
     }  

Copy:

To copy the text data into buffer, we iterate entire selected item collection and keep on appending items into a StringBuilder object.  To keep every item in a new row, we are using here AppendLine()method after inserting item text into stringbuilder. Then at the end we use Clipboard.SetData() method to copy the data into buffer.
     private void copyToolStripMenuItem_Click(object sender, EventArgs e)  
     {  
        try  
       {  
         StringBuilder sb = new StringBuilder();  
         foreach (object row in listBox1.SelectedItems)  
         {  
           sb.Append(row.ToString());  
           sb.AppendLine();  
         }  
         sb.Remove(sb.Length - 1, 1); // Just to avoid copying last empty row  
          Clipboard.SetData(System.Windows.Forms.DataFormats.Text, sb.ToString());  
       }  
       catch (Exception ex)  
       {  
         MessageBox.Show(ex.Message);  
       }  
     }  

Cut:

Basically cut operation is the combination of copying and removing the selected items. In order to do this, we start copying all the selected items (as above) into buffer.
After copying, we remove all the selected items from the List Box.  For this we need another collection to keep all the selected items. We are creating this separate collection because we can’t delete item from the collection while iterating the same collection using foreach loop.

     private void cutToolStripMenuItem_Click(object sender, EventArgs e)  
     {  
       try  
       {  
         StringBuilder sb = new StringBuilder();  
         // we use this collection to keep all the Selected Items  
         List<object> selectedItemList = new List<object>();  
         foreach (object row in listBox1.SelectedItems)  
         {  
           sb.Append(row.ToString());  
           sb.AppendLine();  
           // Keep on adding selected item into a new List of Object  
           selectedItemList.Add(row);  
         }  
         sb.Remove(sb.Length - 1, 1);  // Just to avoid copying last empty row  
         Clipboard.SetData(System.Windows.Forms.DataFormats.Text, sb.ToString());  
         // Removing selected items from the list box  
         foreach (object ln in selectedItemList)  
         {  
           listBox1.Items.Remove(ln);  
         }  
       }  
       catch (Exception ex)  
       {  
         MessageBox.Show(ex.Message);  
       }  
     }  

Clear All:

This is the simplest operation. We need to just clear all the items from List Box using built-in Clear() method.
     private void clearAllToolStripMenuItem_Click(object sender, EventArgs e)  
     {  
       listBox1.Items.Clear();  
     }  

1 comment: