Add items Undo items in Listbox

In this blog we will know how to add items to list box and undo the added items.

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace Listbox_add_undo_items

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void btn_add_Click(object sender, EventArgs e)

        {

            listBox1.SelectedIndex = listBox1.Items.Add(textBox1.Text);

            textBox1.Clear();

        }

 

        private void btn_undo_Click(object sender, EventArgs e)

        {

            if (listBox1.Items.Count > 0)

            {

                listBox1.Items.RemoveAt(listBox1.Items.Count - 1);

            }

        }

    }

}