Click here to Skip to main content
15,943,722 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi.
im trying to Create drag and drop between two Listboxes.
i Add values Into listbox1 And when i want to Drag it into Listbox2 it fails.

i have to mention that There is no database or item binding.
i created text box and i add item from there

Here is my code:

What I have tried:

private void Listbox1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
      {


          listbox1startsmousepose=e.GetPosition(null);


      }

      private void Listbox2_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
      {

          listbox2startsmousepose=e.GetPosition (null);

          }

      private void Listbox1_MouseMove(object sender, MouseEventArgs e)
      {
          Point mPos = e.GetPosition(null);

          if (e.LeftButton == MouseButtonState.Pressed &&
              Math.Abs(mPos.X) > SystemParameters.MinimumHorizontalDragDistance &&
              Math.Abs(mPos.Y) > SystemParameters.MinimumVerticalDragDistance)
          {
              try
              {

                  ListBoxItem selectedItem = (ListBoxItem)Listbox1.SelectedItem;

                  Listbox1.Items.Remove(selectedItem);


                  DragDrop.DoDragDrop(this, new DataObject(DataFormats.FileDrop, selectedItem), DragDropEffects.Copy);


                  if (selectedItem.Parent == null)
                  {
                      Listbox1.Items.Add(selectedItem);
                  }
              }
              catch { }
          }

      }

      private void Listbox2_MouseMove(object sender, MouseEventArgs e)
      {
          Point mPos = e.GetPosition(null);

          if (e.LeftButton == MouseButtonState.Pressed &&
              Math.Abs(mPos.X) > SystemParameters.MinimumHorizontalDragDistance &&
              Math.Abs(mPos.Y) > SystemParameters.MinimumVerticalDragDistance)
          {
              try
              {

                  ListBoxItem selectedItem = (ListBoxItem)Listbox2.SelectedItem;

                  Listbox2.Items.Remove(selectedItem);


                  DragDrop.DoDragDrop(this, new DataObject(DataFormats.FileDrop, selectedItem), DragDropEffects.Copy);


                  if (selectedItem.Parent == null)
                  {
                      Listbox2.Items.Add(selectedItem);
                  }
              }
              catch { }
          }
      }

      private void Listbox2_Drop(object sender, DragEventArgs e)
      {
          if (e.Data.GetData(DataFormats.FileDrop) is ListBoxItem listItem)
          {
              Listbox2.Items.Add(listItem);
          }

      }

      private void Listbox1_Drop(object sender, DragEventArgs e)
      {
          if (e.Data.GetData(DataFormats.FileDrop) is ListBoxItem listItem)
          {
              Listbox1.Items.Add(listItem);
          }
      }
Posted
Updated 8-Jan-22 4:03am
Comments
CHill60 7-Jan-22 10:32am    
"It fails" - can you be more specific?
Member 15489508 7-Jan-22 10:38am    
Yes
I mean I can't drag anything between these two listboxes
[no name] 7-Jan-22 11:43am    
I use "mouse capture". Capture on left mouse down and MouseOver for source; move proxy or control with MouseMove; "drop" when left mouse released on MouseOver target; release mouse.

https://docs.microsoft.com/en-us/dotnet/api/system.windows.uielement.capturemouse?view=windowsdesktop-6.0

1 solution

You're making it way too complicated;)

Here is the absolute minimum that is necessary for the simplest implementation of DragDrop.

a.) Make sure the Drop Traget's (here listBox2) AllowDrop is set to true

b.) This will start the DragDrop operation
C#
private void listBox1_MouseMove(object sender, MouseEventArgs e)
{
    // In case no item is selected job is done
    if (listBox1.SelectedItem == null) return;

    // Start DragDrop 
    if (e.LeftButton == MouseButtonState.Pressed)
    {
        // The data to be passed to the Drop Targent is 'listBox1.SelectedItem' 
        DragDrop.DoDragDrop(sender as ListBox, listBox1.SelectedItem, DragDropEffects.Copy);
    }
}

c.) This will handle the Drop
C#
private void listBox2_Drop(object sender, DragEventArgs e)
{
    // Check whether the passed data is what we expec. In this case ListBoxItem 
    ListBoxItem item = e.Data.GetData(typeof(ListBoxItem)) as ListBoxItem;
    if (item == null) return;

    // Finally add the data to listBox2
    listBox2.Items.Add(item.Content.ToString());

    // Done
    e.Handled = true;
}


For more complex situations where several different DragDrop sources are available you can then fine tune with this
C#
private void listBox2_DragOver(object sender, DragEventArgs e)
{
    if (e.Data.GetData(typeof(ListBoxItem)) as ListBoxItem == null)
    {
        e.Effects = DragDropEffects.None;
    }
    else
    {
        e.Effects = DragDropEffects.Copy;
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900