3

I'm looking for a way to determine which item in a toolStrip that was dragged after a DragDrop event has occured, all I want to do is make a switch case with different cases for each item in the toolstrip but I cant seem to find a way of comparing them.

UPDATE: SHORT CODESAMPLE

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

Hopefully a bit easier to get what I'm trying to do.

jlodenius
  • 829
  • 2
  • 13
  • 24
  • What have you tried? http://mattgemmell.com/2008/12/08/what-have-you-tried/ – Soner Gönül Mar 12 '13 at 14:40
  • You can't do switch on reference types such as UI controls. You will have to do if/else if/else. If you provide some code from your handler and also where your UI is defined (.designer file) we could be of more help. – Paul Sasik Mar 12 '13 at 14:42
  • Didn't think code sample was nessecary since all it is is a toolstrip with 2 buttons, and an empty panel. What I want is to raise a dragdrop event when I drag something from the toolstrip into the panel and then determine which of the items in the toolstrip was draged, for now its just 2 buttons but I can only assume there is a way to compare the text of the buttons in the strip, and I want something like a switch case for each button (or just a coupple of if's if thats what it takes). – jlodenius Mar 12 '13 at 14:50
  • @Jacco: A code sample would keep me from having to recreate your situation in a project, which I am not keen to do. Please create an OnDrop handler in your Panel and write some code, even if it's crap, to signal your intent. Please. – Paul Sasik Mar 12 '13 at 14:53
  • updated the question, hopefully its easier to understand. – jlodenius Mar 12 '13 at 15:04

1 Answers1

3

The events in your example don't look like the correct events to use.

Here is a working example from a ToolStrip that has 2 ToolStripButtons on it:

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");
    }
  }
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225