从工具条拖放

本文关键字:拖放 工具 | 更新日期: 2023-09-27 18:03:36

我正在寻找一种方法来确定在DragDrop事件发生后拖动的工具带中的哪个项目,我所要做的就是为工具带中的每个项目制作不同的案例,但我似乎找不到比较它们的方法。

UPDATE: SHORT codessample

private void toolStrip1_DragDrop(object sender, DragEventArgs e)
{
    //Here I want something like a DoDragDrop() and send the specific item from the
    //toolstrip..
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
    //And here some way to determine which of the items was dragged 
    //(I'm not completely sure if I need a mouseUp event though..)
}

希望能更容易理解我要做的

从工具条拖放

您示例中的事件看起来不像要使用的正确事件。

下面是一个ToolStrip的工作示例,上面有2个ToolStripButtons:

public Form1() {
  InitializeComponent();
  toolStripButton1.MouseDown += toolStripButton_MouseDown;
  toolStripButton2.MouseDown += toolStripButton_MouseDown;
  panel1.DragEnter += panel1_DragEnter;
  panel1.DragDrop += panel1_DragDrop;
}
void toolStripButton_MouseDown(object sender, MouseEventArgs e) {
  this.DoDragDrop(sender, DragDropEffects.Copy);
}
void panel1_DragEnter(object sender, DragEventArgs e) {
  e.Effect = DragDropEffects.Copy;
}
void panel1_DragDrop(object sender, DragEventArgs e) {
  ToolStripButton button = e.Data.GetData(typeof(ToolStripButton))
                           as ToolStripButton;
  if (button != null) {
    if (button.Equals(toolStripButton1)) {
      MessageBox.Show("Dragged and dropped Button 1");
    } else if (button.Equals(toolStripButton2)) {
      MessageBox.Show("Dragged and dropped Button 2");
    }
  }
}