来自 toolStrip 的 C# DragDrop

标签 c# drag-and-drop toolstrip

我正在寻找一种方法来确定在发生 DragDrop 事件后拖动了 toolStrip 中的哪个项目,我想要做的就是为工具条中的每个项目创建一个具有不同案例的切换案例,但我似乎无法找到一种比较它们的方法。

更新:短代码示例

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");
    }
  }
}

关于来自 toolStrip 的 C# DragDrop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15364264/

相关文章:

c# - 我应该使用哪种技术来开发高性能 Web 应用程序

c# - 将单个 XElement 转换为对象

c# - 更改 ToolStripSeparator 控件的 BackColor

c# - 如何自定义 ToolStripTextBox 的呈现?

c# - 以线程安全的方式设置 ToolStripLabel

c# - DotNetZip 更改国家字母

c# - 在 C# 记录中缓存昂贵的重新计算属性或字段

javascript - 如何获得相对于当前帧的结束拖动位置?

objective-c - NSImageView 拖放问题

javascript - 将元素拖到其可滚动父 div 之外 - ( React-Draggable )