In a Windows application are listbox control provide one of
the must 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 .
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.
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
0 comments:
Please let me know your view