C# Add Remove Item From ListBox And Move Items


c# listbox item

C# - How To Add And Remove Item From ListBox And Move Items Up And Down

                                                                                                                         

In This C# Tutorial  We Will See How To Insert Using TextBox , ComboBox And Delete Item From ListBox And Moving Item Up N Down Using CSharp Programming Language.


Project Source Code:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Csharp_And_MySQL
{
    public partial class Csharp_ListBox : Form
    {
        public Csharp_ListBox()
        {
            InitializeComponent();
        }

        private void BTN_ADD_Click(object sender, EventArgs e)
        {
            // add item to listbox from textbox
            listBox1.Items.Add(textBox1.Text);

           // add item to listbox from combobox
            listBox1.Items.Add(comboBox1.SelectedItem.ToString());
        }

         // button move selected listbox item up
        private void BTN_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);
            }
         
        }

        // button move selected listbox item down
        private void BTN_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);
            }
        }

    }
}


      
///////////////OUTPUT:





c# listbox move item



Share this

Related Posts

Previous
Next Post »

3 comments

comments
20 juin 2019 à 23:42 delete

Fantastic java source codes. I tried to purchase java inventory source code from sellfy.com but the platform doesnt support Ghana from the country list.Any help?

Reply
avatar
12 octobre 2021 à 17:38 delete

siuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

Reply
avatar
4 janvier 2022 à 10:16 delete

merci beaucoup de tout se que vous faites pour nous

Reply
avatar