How to Make a Windows Form Application Using C# | ListBox


This is a Listbox made using C# in which we can Insert a word, character, or anything and delete it. And you can also swap the elements from the first list to another and vice versa using the buttons.



Preview:

Full 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 ListboxPr
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            int i = 0;
            listBox1.SelectionMode = SelectionMode.MultiExtended;
            for (i=0;i<listBox1.SelectedItems.Count;i++)
            {
                listBox2.Items.Add(listBox1.SelectedItems[i]);
            }
            
            for (i = listBox1.SelectedIndices.Count - 1; i >= 0; i--)
            {
                listBox1.Items.RemoveAt(listBox1.SelectedIndices[i]);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(textBox1.Text != "")
            {
                listBox1.Items.Add(textBox1.Text);
            }
            else
            {
                MessageBox.Show("No Input");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int i = 0;
            listBox1.SelectionMode = SelectionMode.MultiExtended;
            for(i=listBox1.SelectedIndices.Count-1;i>=0;i--)
            {
                listBox1.Items.RemoveAt(listBox1.SelectedIndices[i]);
            }
           
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (textBox2.Text != "")
            {
                listBox2.Items.Add(textBox2.Text);
            }
            else
            {
                MessageBox.Show("No Input");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int i = 0;
            listBox2.SelectionMode = SelectionMode.MultiExtended;
            for (i = listBox2.SelectedIndices.Count - 1; i >= 0; i--)
            {
                listBox2.Items.RemoveAt(listBox2.SelectedIndices[i]);
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            int i = 0;
            listBox2.SelectionMode = SelectionMode.MultiExtended;
            for (i = 0; i < listBox2.SelectedItems.Count; i++)
            {
                listBox1.Items.Add(listBox2.SelectedItems[i]);
            }

            for (i = listBox2.SelectedIndices.Count - 1; i >= 0; i--)
            {
                listBox2.Items.RemoveAt(listBox2.SelectedIndices[i]);
            }
        }
    }
}

Post a Comment

Previous Post Next Post
Best Programming Books

Facebook

AJ Facebook
Checkout Our Facebook Page
AJ Blogs
Checkout Our Instagram Page