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();  
     }  

Thursday, May 2, 2013

Multiline TextBox scroll to bottom automatically



If we want to have our vertical scroll bar in a TextBox (obviously Multiline TextBox) pointing always to the bottom as we write data in the TextBox, we could do this using another way instead of using ScrollToCaret option.

When we write data in TextBox using textBox1.text = “This is my data… bla blah blah….”, by default  the vertical scroll bar always stays at the top. But if we use AppendText propery of textbox, vertical bar automatically comes to bottom whenever data is written in the text box.

                
 textBox1.AppendText(Environment.NewLine);   
 textBox1.AppendText(sMessage);

Thursday, April 18, 2013

Modifying Data in Tables through a View in SQL Server


Can we modify the table data through SQL View? 

The answer is: Yes !

We can modify the data of an underlying base table through a view, in the same manner as we modify data in a table by using UPDATE, INSERT and DELETE statements or by using the BCP utility and BULK INSERT statement. However, the following restrictions apply to updating views, but do not apply to tables:

·         Any modifications, including UPDATE, INSERT, and DELETE statements, must reference columns from only one base table.

·         The columns that are being modified in the view must reference the underlying data in the table columns directly. They cannot be derived in any other way, such as through:

1.       An aggregate function (AVG, COUNT, SUM, MIN, MAX, GROUPING, STDEV, STDEVP, VAR and VARP).

2.       A computation; the column cannot be computed from an expression using other columns. Columns formed using set operators (UNION, UNION ALL, CROSSJOIN, EXCEPT, and INTERSECT) amount to a computation and are also not updatable.

·         The columns that are being modified cannot be affected by GROUP BY, HAVING, or DISTINCT clauses.

·         TOP cannot be used anywhere in the select_statement of the view when WITH CHECK OPTION is also specified.