【问题标题】:C# DragDrop from toolStripC# DragDrop 从 toolStrip
【发布时间】:2013-03-12 14:39:50
【问题描述】:

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

更新:短代码示例

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..)
}

希望更容易得到我想要做的事情。

【问题讨论】:

  • 您无法打开 UI 控件等引用类型。你将不得不做 if/else if/else。如果您提供一些来自处理程序的代码以及定义 UI 的位置(.designer 文件),我们可能会提供更多帮助。
  • 不认为代码示例是必要的,因为它只是一个带有 2 个按钮和一个空面板的工具条。我想要的是在我将工具条中的某些内容拖到面板中时引发一个拖放事件,然后确定工具条中的哪些项目被拖动,现在它只有 2 个按钮,但我只能假设有一种方法来比较条中按钮的文本,我想要每个按钮的开关盒(或者只是几个 if's 如果这就是它所需要的)。
  • @Jacco:代码示例可以让我不必在项目中重新创建您的情况,我不想这样做。请在您的面板中创建一个 OnDrop 处理程序并编写一些代码,即使它是废话,以表明您的意图。请。
  • 更新了问题,希望它更容易理解。

标签: c# drag-and-drop toolstrip


【解决方案1】:

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

这是一个来自 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");
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-05
    • 1970-01-01
    • 2014-03-02
    • 1970-01-01
    相关资源
    最近更新 更多