• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

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

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.

The cursor arrow icon becomes "not allowed" icon.

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:

cbreemer

King Coder
I think your problem is that you are using the listBox2_DragOver event rather than the listBox2_DragEnter event. Changing that made your code work for me - the value is now inserted in the second listbox.
What doesn't work is the deletion from the first listbox. Your DoDragDrop call returns None, whereas you expected it to return DragDropEffects.All. I must admit I don 't know why, or even understand the role of DragDropEffects here. But personally I would not delete the value from the first listbox the moment you press the mouse button, that seems a bit premature. I suggest you do that after you have successfully inserted the value in the second listbox. This requires that you remember the x,y values from the MouseDown event, or pass them along with the value of deger.

I hope this helps.
 
Last edited:
Top