0
votes

done

ListViews have itemDrag but ListBoxes miss that event so.. how can I achieve same funcionality ?

UPDATE

Doing the same thing between ListBoxes and ListView, I see it's almost the same:

  • MouseDown / ItemDrag --> call DoDragDrop() to enable drag and drop events

  • DragEnter --> place to put effects (Move, Copy, None, ..)

  • DragDrop --> here I move / copy the elements

1
It appears it is possible to Drag and Drop with ListBox. See stackoverflow.com/questions/16591975/…GEEF
Thanks @VP : I'll check it!boctulus
Indeed it is missing. But with DragDrop, -Enter, -Leave, - Over.. you can do whatever you want, you just will have to take care of the SelectedIndex -Item yourself.. - OTOH, while this is not really a compelling reason to switch to ListVIew, it is more or less always the better choice!TaW
Thanks to all...... "solved"boctulus

1 Answers

0
votes

Thanks for the comments (short answers), finally I could achieve my goal

    // (from,to) ListBox 
    private void moveItems(ListBox lbA, ListBox lbB)
    {
        lbB.Items.Add(selected);
        lbA.Items.RemoveAt(index);
    }


    // (from,to) ListBox 
    private void moveItemsByName(string lbName1, string lbName2)
    {
        ListBox lb1 = Controls[Controls.IndexOfKey(lbName1)] as ListBox;
        ListBox lb2 = Controls[Controls.IndexOfKey(lbName2)] as ListBox;

        if (lb1.SelectedItem != null)
        {
            selected = lb1.SelectedItem.ToString();
            index = lb1.SelectedIndex;
            moveItems(lb1, lb2);                
        }
    }


    #region DoubleClick
    /*         
     *  Send items by DoubleClick
     */
    private void listBox_DoubleClick(object sender, EventArgs e)
    {
        ListBox lb_sender = sender as ListBox;
        moveItemsByName(lb_sender.Name, (lb_sender.Name == "listBox1" ? "listBox2" : "listBox1"));
    }
    #endregion

    #region Drag-and-drop
    /*
     *  Send items dragging them
     */
    private void listBox_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Clicks > 1)
            return;

        ListBox lb_sender = sender as ListBox;

        if (lb_sender.SelectedItem != null)
        {
            selected = lb_sender.SelectedItem;
            index = lb_sender.SelectedIndex;
            control = lb_sender.Name;
            DoDragDrop(selected.ToString(), DragDropEffects.Move);
        }
        else
            index = -1;
    }

    private void listBox_DragEnter(object sender, DragEventArgs e)
    { e.Effect = DragDropEffects.Move; }

    private void listBox1_DragDrop(object sender, EventArgs e)
    {   
        if (control == "listBox2" && index != -1)
        {
            moveItems(listBox2, listBox1);
        } 
    }

    private void listBox2_DragDrop(object sender, EventArgs e)
    {
        if (control == "listBox1" && index != -1)
        {
            moveItems(listBox1, listBox2);
        } 
    }
    #endregion        

    #region forward / backward arrows
    /*
     *  Send items by clicking arrows
     */
    private void lbArrow_Click(object sender, EventArgs e)
    {
        if (sender.Equals(lbArrowForward))
            moveItemsByName("listBox1", "listBox2");
        else
            moveItemsByName("listBox2", "listBox1");
    }
    #endregion

}