Friday, 23 May 2014

How to add/move multiselected items from one listbox to another listbox in C#.Net and VB.Net | C# multiple selection listbox move Using C#.Net

5/23/2014 - By Pranav Singh 1

In a Windows application are listbox control provide one of the mst important part to provide data in a list format  In this article I will show you how you cab transfer listbox items to another listbox c#.net and vb.net.

Some of my previous articles are as follows: How to show error message in c# windows application | How to show Error & Warning Message Box in .NET . Here is the article in which I have shown how to move single item at a time from one listbox to another listbox Transfer Listbox Items to Another Listbox Using C#.Net and VB.Net | How to Move List Box Items to another List Box in C# .

Some of the eBooks which you may also like to download : How to show error message in c# windows application | How to show Error & Warning Message Box in .NET.

So for this article first you create a new windows application and add two listbox control and two button control to transfer the records.


So for making multiple selections possible you have to just set the listbox control property as shown below.




Here is the code to transfer the item from one listbox to another listbox. In this we have written code on button1 and button 2 click.
C#.Net
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;
using System.Collections;

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

        private void btnright_Click(object sender, EventArgs e)
        {
            foreach (var item in new ArrayList(listBox1.SelectedItems))
            {
                listBox1.Items.Remove(item);
                listBox2.Items.Add(item);
            }
        }

        private void btnleft_Click(object sender, EventArgs e)
        {
            foreach (var item in new ArrayList(listBox2.SelectedItems))
            {
                listBox2.Items.Remove(item);
                listBox1.Items.Add(item);
            }
        }
    }
}


VB.Net
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports System.Collections

Namespace ProjectDemo_Windows
    Partial Public Class Form1
        Inherits Form
        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub btnright_Click(ByVal sender As Object, ByVal e As EventArgs)
            For Each item As var In New ArrayList(listBox1.SelectedItems)
                listBox1.Items.Remove(item)
                listBox2.Items.Add(item)
            Next
        End Sub

        Private Sub btnleft_Click(ByVal sender As Object, ByVal e As EventArgs)
            For Each item As var In New ArrayList(listBox2.SelectedItems)
                listBox2.Items.Remove(item)
                listBox1.Items.Add(item)
            Next
        End Sub
    End Class
End Namespace

In above code we have applied loop to check which item in selected and if it’s selected on that case we are removing item from listbox1 and adding it to listbox2.

So here is the final output:

 

DOWNLOAD


About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

1 comment:

  1. vry nice
    vry neatly explained
    u may also read this:
    http://www.mindstick.com/Articles/5d02d2bc-82f8-4543-8780-f03a37b5f0aa/Move%20Items%20from%20one%20ListBox%20to%20another%20in%20CSharp%20NET

    ReplyDelete

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top