Context Menue Strip Qyuestion

Z

Zach

Jeff Johnson said:
Okay, I use VS2005 as well, so I'll try a test. However, I'm using XP, not
Vista, so we may still get different results.
ooooooooooooooooooooooooooooooooooooooooooooooooooo
In my XO box no problem.
Is it a Vista freak?

using System;

using System.Windows.Forms;

namespace TestOps

{

public partial class Form1 : Form

{

int x;

int y;

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

listView1.MouseDown +=new MouseEventHandler(listView1_MouseDown);

x = this.Location.X;

y = this.Location.Y;

}


void listView1_MouseDown(object sender, MouseEventArgs e)

{


contextMenuStrip1.Show(x + 50,y + 50);

}

}

}
 
J

Jeff Johnson

ooooooooooooooooooooooooooooooooooooooooooooooooooo
I could replicate a result you get on an XP box as well.
Zach.

Here are my findings.

[Setup]

I created a simple Windows Application project. On Form1 I placed a list
view control (listView1). I added three items (Item 1, Item 2, Item 3) in
design mode. I did this so that I could right-click on both items and the
empty space of the list view. I also added a ContextMenuStrip to the form
(contextMenuStrip1) and put three menu items in it. For display purposes
only I named them Cut, Copy, and Paste (there's no functionality behind
them).

[Case 1]

Changes: I set the listView1.ContextMenuStrip = contextMenuStrip1.

Result: When I right-click on the list view, contextMenuStrip1 appears with
its top-left corner directly under my mouse pointer.

[Case 2]

Changes: I created an event handler for listView1.MouseDown and added this
code to it:

if (e.Button == MouseButtons.Right)
{
contextMenuStrip1.Show(100, 100);
}

I did not alter listView1.ContextMenuStrip. It is still set to
contextMenuStrip1.

Result: Clicking the right mouse button down immediately causes the context
menu to appear at physical screen point (100, 100). When I release the
button, the menu disappears and reappears under my mouse pointer.

[Case 3]

Changes: I set listView1.ContextMenuStrip = (none).

Result: The context menu appears at (100, 100) on mouse down and does not
disappear or move on mouse up.

[Case 4]

Changes: I removed the MouseDown event handler and added a handler for
MouseUp with the exact same code as MouseDown. listView1.ContextMenuStrip is
still set to (none).

Result: The menu does not appear until mouse up, at which time it appears at
(100, 100) and does not disappear or move.

[Case 5]

Changes: I set listView1.ContextMenuStrip = contextMenuStrip1 again.

Result: On mouse up, the menu flickers almost imperceptibly at (100, 100)
and then appears "for real" under the mouse pointer.


From this test, the only way to replicate the behavior you mentioned earlier
was to have BOTH the code (in an event handler) and Windows Forms (via the
ContextMenuStrip property) displaying the context menu. In that situation,
Windows Forms wins, and the context menu appears under the mouse pointer.
All I can suggest at this point is to check very carefully to make sure that
you have not set the ContextMenuHandler property for your list view.
 
J

Jeff Johnson

In my XO box no problem.
Is it a Vista freak?

Could be. Yet another reason to love Vista....

If I think about it over the weekend (and that's not likely, but maybe I'll
get reeeeeeealy bored), I'll test it on Vista at home.

Let me clarify one thing first: the context menu that appears at (100, 100)
and then appears under your mouse cursor is the SAME menu, right? In other
words, it's the menu you created and not some built-in menu? (I can't
imagine even Vista being dumb enough to have a built-in menu for a list
view, though. List views are far too generic for the concept of a "global"
context menu.)
 
Z

Zach

Jeff Johnson said:
Okay, I use VS2005 as well, so I'll try a test. However, I'm using XP, not
Vista, so we may still get different results.
oooooooooooooooooooooooooooooooooooooooooooooo
Well now. You will have seen my test code.
On XP it produces a menu at the desired location
irrespective of which mouse button (right or left),
whereas in Vista, it will produce a menu at the
desired location with a left click only, whereas
a right click will produce a menu where the
cursor is. I should say though that I have
Framework 3.5 on the XP box, and 3.0
on the Vista Box.

Zach.
 
Z

Zach

Jeff Johnson said:
Could be. Yet another reason to love Vista....

If I think about it over the weekend (and that's not likely, but maybe
I'll get reeeeeeealy bored), I'll test it on Vista at home.

Let me clarify one thing first: the context menu that appears at (100,
100) and then appears under your mouse cursor is the SAME menu, right? In
other words, it's the menu you created and not some built-in menu? (I
can't imagine even Vista being dumb enough to have a built-in menu for a
list view, though. List views are far too generic for the concept of a
"global" context menu.)
ooooooooooooooooooooooooooooooooooooooooooooooooooo
You have posted some interesting stuff Jeff.
The break through as far as my application is concerned was
produced by your line

listView1.ContextMenuStrip = null;

I have a mouseUp event handler in place.
I am not checking for right button.
I get the menu in the desired position whichever
mouse button I press, and for other reasons I
like that to happen.

I don't get the menu showing at the cursor position.

All in all the outcome is brilliant.

Many thanks,
Have a nice weekend,
Zach.
 
J

Jeff Johnson

ooooooooooooooooooooooooooooooooooooooooooooooooooo
You have posted some interesting stuff Jeff.
The break through as far as my application is concerned was
produced by your line

listView1.ContextMenuStrip = null;

I have a mouseUp event handler in place.
I am not checking for right button.
I get the menu in the desired position whichever
mouse button I press, and for other reasons I
like that to happen.

I don't get the menu showing at the cursor position.

All in all the outcome is brilliant.

Cool!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top