ButtonCluster control means which contains four button controls helps to move items from left to right and right to left. In this article, we discuss how to create ButtonCluster in C# Win Forms.
Open Microsoft Visual Studio 2015 => Create C# Windows Forms application and name it as ButtonClusterExp. Add four Button controls and two Listboxes as shown below.
Here we have added ten items to ListBox. Now add the required code for all these button controls as shown below.
using System;
using System.Windows.Forms;
namespace ButtonClusterExp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnAllRight_Click(object sender, EventArgs e)
{
foreach (var item in lbSource.Items)
{
lbDestination.Items.Add(item);
}
lbSource.Items.Clear();
}
private void btnRight_Click(object sender, EventArgs e)
{
foreach (var item in lbSource.SelectedItems)
{
lbDestination.Items.Add(item);
}
if (lbSource.SelectedItems.Count != 0)
{
while (lbSource.SelectedIndex != -1)
{
lbSource.Items.RemoveAt(lbSource.SelectedIndex);
}
}
}
private void btnLeft_Click(object sender, EventArgs e)
{
foreach (var item in lbDestination.SelectedItems)
{
lbSource.Items.Add(item);
}
if (lbDestination.SelectedItems.Count != 0)
{
while (lbDestination.SelectedIndex != -1)
{
lbDestination.Items.RemoveAt(lbDestination.SelectedIndex);
}
}
}
private void btnAllLeft_Click(object sender, EventArgs e)
{
foreach (var item in lbDestination.Items)
{
lbSource.Items.Add(item);
}
lbDestination.Items.Clear();
}
}
}
As shown above we have four buttons; first is to move all items from left list box to right listbox, second is to move selected items from left listbox to right listbox, third is to move selected items from right to left, and the fourth one is to get all right listbox items to left listbox items.