In this example, we will create an C# Form example that transfer a data from a listbox to another listbox.
we will also learn how to remove the copied data from “listbox1”.
If listbox rows is not selected, a messagebox will shown to user for selecting an item.
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace listbox1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { if (listBox1.SelectedIndex!=-1) { listBox2.Items.Add(listBox1.SelectedItem); listBox1.Items.Remove(listBox1.SelectedItem); } else { MessageBox.Show("Choose an item."); } } private void button2_Click(object sender, EventArgs e) { if (listBox2.SelectedIndex != -1) { listBox1.Items.Add(listBox2.SelectedItem); listBox2.Items.Remove(listBox2.SelectedItem); } else { MessageBox.Show("Choose an item."); } } } } |