473,387 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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(fals e);
Application.Run(new Form1());
}
}
}

Thank you in advance,
Andy
--
You can email me by removing the NOSPAM parts below:
xm**********@gmxNOSPAM.net
Sep 18 '07 #1
2 5251
Why not use a timer with a small interval of say 100ms and call the Show
method in timer.tick?

---------
- G Himangi, Sky Software http://www.ssware.com
Shell MegaPack : GUI Controls For Drop-In Windows Explorer like Shell
Browsing Functionality For Your App (.Net & ActiveX Editions).
EZNamespaceExtensions.Net : Develop namespace extensions rapidly in .Net
EZShellExtensions.Net : Develop all shell extensions,explorer bars and BHOs
rapidly in .Net
---------
"Andreas Mueller" <me@privacy.netwrote in message
news:5l************@mid.individual.net...
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(fals e);
Application.Run(new Form1());
}
}
}

Thank you in advance,
Andy
--
You can email me by removing the NOSPAM parts below:
xm**********@gmxNOSPAM.net

Sep 19 '07 #2
G Himangi wrote:
Why not use a timer with a small interval of say 100ms and call the Show
method in timer.tick?
Hi,

the context menu itself shows up fine. The problem is that when I click
on the menu, the handler is executed *after* the Drag and Drop operation
has finished.

Thanks,
Andy
--
You can email me by removing the NOSPAM parts below:
xm**********@gmxNOSPAM.net
Sep 19 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Francesco | last post by:
Hello Pythonnian's (sorry for crossposting) I have written on the base of Miro Rajic a (still) small filemanager in wxPython. Now I want to add (for Windows XP) a explorer shell context menu....
2
by: Michel | last post by:
I like to show a inbetween selfmade menu when rightclicking the page. So on the first line I want to add "Default right-click menu". So first I need to capture it, then I need to stop the...
2
by: Paul Ledger | last post by:
I want to be able to display a context menu when the user righthand drags an item into a textbox. It states in the documentation that the e.KeyState property will be set accordingly in the drop...
0
by: gurjinder | last post by:
Hi, I am making one component in that i am inherting from System.ComponentModel.Component i am trying to make a component like sqldataadapter . when we drag that component to our form in...
3
by: Marcel Hug | last post by:
Hello NG ! I would like to show the context menu which apears when i drop a file by fight mouse button. Could somebody help me ? Thanks
0
by: Alvo von Cossel I | last post by:
hi, i have a context menu with the Drop-shadow propery set to false. it works for the main part of the menu but in a submenu it doesnt. is there any way to stop this apart from creating another...
1
by: cool_jaggs | last post by:
Hi , I am new to this group, but it would be really nice if you can really help me out. I am trying to Export a file from my application to windows explorer through drag and drop using right...
1
by: avanti | last post by:
Hi, I am trying to show a context menu on a right click in some of my controls. I want to show it at the right location. However it is getting shown away from the mouse click location. Here is...
0
by: arvinds | last post by:
I have an application in which I have 2 forms. 1.Review Form 2. FilmForm. Review Form is used for Loading some images and we can transfer the Images from Review From to FilmForm by Drag-Drop...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.