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# .
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
vry nice
ReplyDeletevry 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