简体   繁体   中英

How can I drag and drop from Excel cells to a drag-enabled task pane programatically?

I am using Excel 2007, VS2008 Pro. I am building a VSTO Add-in that requires "drag and drop from Excel cells to a drag-enabled task pane".

So far I notice that I can only drag and drop within the cells themselves. It does not allow me to drop into the task Pane or drag past the sheet limits. (http://www.computerimages.com/tip_xl.html)

Note: The task pane has drag drop enabled, I ahve already tested I can drag/drop from task pane to excel but I need to do this the other way around?

From the IDE, set AllowDrop on the control you want to drop your data on, then wire up the events for DragOver and DragDrop on that same control.

Your code would look something like this:

TextBox TaskPane;

void DragNDrop(object sender, DragEventArgs e) {
  if (e.Effect == DragDropEffects.Move) {
    if (e.Data.GetDataPresent(DataFormats.CommaSeparatedValue)) {
      string csvText = e.Data.GetData(DataFormats.CommaSeparatedValue, false).ToString();
      if (!String.IsNullOrEmpty(csvText)) {
        TaskPane.Text = csvText;
      }
    }
  }
}

void DragOver(object sender, DragEventArgs e) {
  if (!e.Data.GetDataPresent(DataFormats.CommaSeparatedValue)) {
    e.Effect = DragDropEffects.None;
  } else {
    e.Effect = DragDropEffects.Move;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM