.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Visual Studio >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Move items from one list box to other and vice-versa

Posted By:satyapriyanayak       Posted Date: July 02, 2013    Points: 40    Category: Visual Studio    URL: http://www.dotnetspark.com  

Move items from one list box to other and vice-versa
 

Here we will move items from one list box to other and vice-versa. But if first list box contains some duplicate values it will not allowed to move, rather we will get a message item exits.

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

        private void Form1_Load(object sender, EventArgs e)
        {
            listBox1.Items.Add("Raj");
            listBox1.Items.Add("Raj");
            listBox1.Items.Add("Raju");
            listBox1.Items.Add("Rahul");
            listBox1.Items.Add("Harry");
            listBox1.Items.Add("Ben");
            listBox1.Items.Add("Ian");

        }

        

       
        private void btn_right_Click(object sender, EventArgs e)
        {
            if (listBox2.Items.Contains(listBox1.SelectedItem) == true)
            {
                MessageBox.Show("Item Exits");
            }
            else
            {
                listBox2.Items.Add(listBox1.SelectedItem);
                int i = 0;
                i = listBox1.SelectedIndex;
                listBox1.Items.RemoveAt(i);

            }
        }

        private void btnleft_Click(object sender, EventArgs e)
        {
            listBox1.Items.Add(listBox2.SelectedItem);
            int i = 0;
            i = listBox2.SelectedIndex;
            listBox2.Items.RemoveAt(i);
        }
    }
}



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend