How to add , delete and get value of listbox in C#.Net


In this article, I would like to show you how to add items, remove selected  item, delete all items, and get value from selected item in  listbox. 
Sample form
Sample form

 1. Add value to listbox

Private void btnAdd_Click(object sender, EventArgs e)
{
    //add value from textbox to listbox
    listBox1.Items.Add(textBox1.Text);
} 

2. Remove selected value in listbox 

       Private void btnRemove_Click(object sender, EventArgs e)
       {
     //get index of selected item of listbox 
     Int ind = listBox1.SelectedIndex;
    //remove selectd item of listbox
           listBox1.Items.RemoveAt(ind);
        }

3. Get selected value from listbox  

      private void btnDisplay_Click(object sender, EventArgs e)
      {
        //get index of selected item of listbox
        int ind = listBox1.SelectedIndex;
        //display value of selected item of listbox in label
        label1.Text = listBox1.Items[ind].ToString();
      }

 4. Delete all items in listbox

      private void btnClear_Click(object sender, EventArgs e)
      {
          //clear all items in listbox
          listBox1.Items.Clear();
      }

Enjoy it ....Hope this post is helpful for you.

How to add , delete and get value of listbox in C#.Net How to add , delete and get value of listbox in C#.Net Reviewed by BeiLover on 6:39 PM Rating: 5

No comments:

Powered by Blogger.