【问题标题】:TEDrag and drop between ListBoxes in C#在 C# 中的 ListBox 之间拖放
【发布时间】:2014-12-16 01:55:40
【问题描述】:

ListViews 有 itemDrag 但 ListBoxes 错过了该事件,所以.. 我怎样才能实现相同的功能?

更新

在 ListBoxes 和 ListView 之间做同样的事情,我看到它几乎是一样的:

  • MouseDown / ItemDrag --> 调用 DoDragDrop() 以启用拖放事件

  • DragEnter --> 放置效果的位置(移动、复制、无、..)

  • DragDrop --> 我在这里移动/复制元素

【问题讨论】:

  • 似乎可以使用 ListBox 进行拖放。见stackoverflow.com/questions/16591975/…
  • 谢谢@VP:我会检查的!
  • 确实不见了。但是有了DragDrop, -Enter, -Leave, - Over..你可以做任何你想做的事,你只需要自己照顾SelectedIndex -Item.. - OTOH,虽然这并不是切换到ListVIew的真正令人信服的理由,但更多的是或更少总是更好的选择!
  • 感谢大家......“解决了”

标签: c# listview events drag-and-drop listbox


【解决方案1】:

感谢cmets(简短的回答),终于可以实现我的目标了

    // (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

}

【讨论】:

  • 正在工作......除了......这个解决方案与 DoubleClick 事件不兼容,因为 DragDrop 事件会干扰在 listBoxes 之间发送项目。一体机更复杂
  • 您可以在 MouseDown 事件中检查 DoubleClick:if (e.Clicks > 1) {}
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多