Dragging an item from listBox1 and droping the item into listBox2 code does not work(AllowDrop=true)

Joined
Sep 26, 2022
Messages
16
Programming Experience
1-3
Although both of the listBoxes **AllowDrop=true** listBox2 does not allow me to drop an item from listbox1 into listBox2.

VS 2022 does not give any error, warning or exception handling problem. The problem is that this code does not do what it supposed to do. It does not let me carry 1 item from listBox1 to listBox2.

C#:
namespace LISTBOX_fareileSURUKLEbirakDRAGDROP_Olaylari
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
            listBox1.AllowDrop = true;
            listBox2.AllowDrop = true;

            int i;
            for(i = 0; i < 10; i++)
            {
                listBox1.Items.Add(i);
            }
        }

        private void listBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (listBox1.Items.Count == 0) return;

            string deger = listBox1.Items[listBox1.IndexFromPoint(e.X,e.Y)].ToString();

            if (DoDragDrop(deger, DragDropEffects.All) == DragDropEffects.All)
                listBox1.Items.RemoveAt(listBox1.IndexFromPoint(e.X, e.Y));
        }
        private void listBox2_DragOver(object sender,DragEventArgs e)
        {
            e.Effect= DragDropEffects.All;
        }
        private void listBox2_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.StringFormat))
                listBox2.Items.Add(e.Data.GetData(DataFormats.StringFormat));
        }
    }
}

UI:
1665140233732.png


What do you think is the problem here?
 
Last edited:
The code works fine here. Is the event handlers actually attached? Look in Properties window.
 
Solution
Back
Top Bottom