C# İki ListBox Arasında Veri Transferi

sdkbyrm

webmasterfrm
Üyelik Tarihi
23 Aralık 2020
Mesajlar
813
Beğeniler
1
Ticaret: 0 / 0 / 0
Bu örneğimizde, C# Windows Form ile Form içine iki ListBox yerleştireceğiz. Bu iki listBox içinde seçilen elemanın diğer ListBox’ a aktarılmasını sağlayacağız. Ayrıca aktarılan elemanın bulunduğu ListBox’ tan nasıl kaldırılacağını öğrenmiş olacağız. Eğer ListBox’ ta hiçbir eleman seçilmemişse bunun kontrolünü gerçekleştirerek kullanıcıya seçim yapmasının gerekli olduğu bir mesaj göstereceğiz.


listbox1-to-listbox2-2.gif




C# Kodları:


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
54

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)
{
//Eleman seçilmeden aktarmak istenirse hata veriyor.
//Bu hatayı engelleyelim.

if (listBox1.SelectedIndex!=-1)
{
listBox2.Items.Add(listBox1.SelectedItem); // Seçili olanı aktar
listBox1.Items.Remove(listBox1.SelectedItem); //Seçili olanı sil
}
else
{
MessageBox.Show("Lütfen seçim yapın.");
}



}

private void button2_Click(object sender, EventArgs e)
{
if (listBox2.SelectedIndex != -1)
{
listBox1.Items.Add(listBox2.SelectedItem); // Seçili olanı aktar
listBox2.Items.Remove(listBox2.SelectedItem); //Seçili olanı sil
}
else
{
MessageBox.Show("Lütfen seçim yapın.");
}
}
}
}
 
Üst