banner



How To Select All Items In A List Box

  • Date May 06, 2013
  • 182.6k
  • 3

In this article, I talk over all the clipboard operations on a ListBox.

Have you ever imagined our life without the clipboard ("Cut-Re-create-Paste") in today's computer era? Especially a Software Developer's life! These clipboard operations are very common and very frequently used, but here I discuss how to lawmaking them (on a ListBox command) instead of only using them.

In this article, I volition show some edit operations, like: Select All, Clear All, Cut, Copy and Paste on a ListBox control. To demonstrate this, I created a simple Win form awarding with a Bill of fare Strip and a ListBox control. On the Menu Strip, all the operations are entered equally menu items with brusque cut keys.

For amend agreement, the sample code is also attached with this article. Hither is a screenshot of the GUI:

image1.png

Select All

To select all the items in a ListBox, we beginning articulate all the selected items and then select each item while iterating through the unabridged list.

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

Paste

To paste the text data into our ListBox, we showtime retrieve the text data from the buffer using the Clipboard form. Since it is a ListBox where each item in the ListBox should represent to each line in the buffer, here we parse the Text data by a new line character and then store each line as a new item in the ListBox.

individual void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
endeavour
      {
// Getting Text from Prune lath
string s = Clipboard.GetText();
//Parsing criteria: New Line
string[] lines = s.Divide('\n');
foreach (string ln in lines)
            {
                 listBox1.Items.Add(ln.Trim());
            }
       }
catch (Exception ex)
       {
MessageBox.Bear witness(ex.Message);
        }
}

Copy

To copy the text information into a buffer, nosotros iterate the entire selected detail drove and continue to append items into a StringBuilder object. To go along every detail in a new row, we are using hither AppendLine()method after inserting the item'south text into a stringbuilder. Then at the cease we use the Clipboard.SetData() method to copy the data into the buffer.

private void copyToolStripMenuItem_Click(object sender, EventArgs due east)
{
endeavour
      {
StringBuilder sb = new StringBuilder();
foreach (object row in listBox1.SelectedItems)
            {
                 sb.Suspend(row.ToString());
                 sb.AppendLine();
            }
            sb.Remove(sb.Length - 1, 1);
// Just to avoid copying final empty row
Clipboard.SetData(System.Windows.Forms.DataFormats.Text, sb.ToString());
       }
catch (Exception ex)
       {
MessageBox.Show(ex.Message);
       }
}

Cut

Basically a cutting operation is the combination of copying and removing the selected items. In order to do this, we kickoff copying all the selected items (as above) into the buffer.

Afterward copying, we remove all the selected items from the ListBox. For this we need another collection to keep all the selected items. We are creating this separate collection because we can't delete items from the drove while iterating the aforementioned collection using a foreach loop.

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

Articulate All

This is the simplest functioning. We need to merely clear all the items from the ListBox using the built-in Clear() method.

individual void clearAllToolStripMenuItem_Click(object sender, EventArgs east)
{
     listBox1.Items.Clear();
}


Source: https://www.c-sharpcorner.com/UploadFile/0f68f2/cut-copy-paste-select-all-clear-all-on-a-listbox/

0 Response to "How To Select All Items In A List Box"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel