Showing a context menu during drag and drop



Hi All,

I'm trying to show a context menu during a drag drop operation similar to the windows explorers right click drag and drop behavior (A full working sample is at the end of the post):

void treeView1_DragDrop(object sender, DragEventArgs e)
{
ContextMenu mnu = new ContextMenu();
mnu.MenuItems.Add("Click me", OnCmClick);
mnu.Show(this, PointToClient(MousePosition));
}
private void OnCmClick(object sender, EventArgs e)
{
Debug.WriteLine("Click");
}



It works, but the OnCmClick handler of the context menu is executed *after* the drag and drop has finished. I think this is because the click message is enqueued into the message queue behind the currently active drag and drop handler, as a call to Application.DoEvents() solves the problem.

As I'm burned by re entrance issues, I'm very reluctant to call DoEvents.

Is there any other way to solve this issue or to implement this behavior?


Here's a complete sample:

using System;
using System.Diagnostics;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

namespace WindowsApplication4
{
public partial class Form1 : Form
{
private readonly TreeView treeView1 = new TreeView();

public Form1()
{
InitializeComponent();
Thread.CurrentThread.Name = "GUI";
treeView1.Nodes.Add("A Node");
treeView1.ItemDrag += treeView1_ItemDrag;
treeView1.DragOver += treeView1_DragOver;
treeView1.DragDrop += treeView1_DragDrop;
}

private void InitializeComponent()
{
SuspendLayout();
treeView1.AllowDrop = true;
treeView1.Location = new Point(10, 10);
treeView1.Name = "treeView1";
treeView1.Size = new Size(120, 200);
treeView1.TabIndex = 0;
ClientSize = new Size(300, 250);
Controls.Add(treeView1);
Name = "Form1";
Text = "Form1";
ResumeLayout(false);
}

private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
{
Debug.WriteLine("--->Started DragDrop");
Debug.WriteLine("Thread: " + Thread.CurrentThread.Name);
DoDragDrop(e.Item, DragDropEffects.All | DragDropEffects.Link);
Debug.WriteLine("--->Finished DragDrop");
}

private void treeView1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}

private void treeView1_DragDrop(object sender, DragEventArgs e)
{
Debug.WriteLine("Thread: " + Thread.CurrentThread.Name);
ContextMenu mnu = new ContextMenu();
mnu.MenuItems.Add("Click me", OnCmClick);
mnu.Show(this, PointToClient(MousePosition));
//Application.DoEvents();// Good Idea??
}

private void OnCmClick(object sender, EventArgs e)
{
Debug.WriteLine("Click");
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}



Thank you in advance,
Andy
--
You can email me by removing the NOSPAM parts below:
xmen40NOSPAM@xxxxxxxxxxxxx
.



Relevant Pages

  • Re: Showing a context menu during drag and drop
    ... Shell MegaPack: GUI Controls For Drop-In Windows Explorer like Shell ... I'm trying to show a context menu during a drag drop operation similar to ... void treeView1_DragDrop(object sender, DragEventArgs e) ... private void OnCmClick ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: Performance is bad
    ... private void Form1_MouseDown(object sender, ystem.Windows.Forms.MouseEventArgs e) ... > I create a circle image form, drag and drop this image on the screen, if I drag this circle fast to the right, the left part of the image is cut, looks to me the ...
    (microsoft.public.dotnet.languages.csharp)
  • VS 2008 and context menus
    ... I have a class which includes a context menu. ... The variable renameSwatchMenuItem is either undeclared or was never assigned. ... private void m_contextMenu_Popup ... if (sender is MenuItem) ...
    (microsoft.public.dotnet.languages.csharp)
  • Drag and Drop multiple items between List boxes
    ... I am trying to drag and drop multiple items from one list box to ... private void lstTo_DragOver(object sender, ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • Re: Newbie Q: How to determine WHICH item was clicked on context menu
    ... private void contextMenuStrip1_MouseClick(object sender, ... MouseEventArgs e) ... items that are found on my Context Menu that the User clicked. ...
    (microsoft.public.dotnet.languages.csharp)