List of Exercises

Add, Remove or Insert Item to ListBox in C#

	
namespace Add_And_Remove_Item_From_ListBox
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void Add_Click(object sender, EventArgs e)
    {
      if (textBox1.Text != "")
      listBox1.Items.Add(textBox1.Text);
    }

    private void insert_index_Click(object sender, EventArgs e)
    {
      if (textBox1.Text != "" & textBox2.Text != "")
        listBox1.Items.Insert(Convert.ToInt16(textBox2.Text) - 1, textBox1.Text);
    }

    private void UP_Click(object sender, EventArgs e)
    {
      int i = listBox1.SelectedIndex;
      string item = listBox1.SelectedItem.ToString();
      if (i > 0)
      {
        listBox1.Items.RemoveAt(i);
        listBox1.Items.Insert(i - 1, item);
        listBox1.SetSelected(i - 1, true);
      }
    }

    private void Down_Click(object sender, EventArgs e)
    {
      int i = listBox1.SelectedIndex;
      string item = listBox1.SelectedItem.ToString();
      if (i < listBox1.Items.Count - 1)
      {
        listBox1.Items.RemoveAt(i);
        listBox1.Items.Insert(i + 1, item);
        listBox1.SetSelected(i + 1, true);
      }
    }

    private void remove_Click(object sender, EventArgs e)
    {
      if (textBox1.Text != "")
        listBox1.Items.Remove(textBox1.Text);
    }

    private void Remove_at_Click(object sender, EventArgs e)
    {
      if (textBox2.Text != "")
        listBox1.Items.RemoveAt(Convert.ToInt16(textBox2.Text) - 1);
    }
  }
}

	


Download the project of Visual Studio 2013 in DropBox Download


How To Add And Remove Item From ListBox in C# | Move ListBox Item Up & Down


Video Thumbnail