Context Menue Strip Qyuestion

Z

Zach

I show a menu strip on screen, at start-up, like so:
contextMenuStrip1.Show();
but it shows up in the top left hand corner.

Question, how do I get it to show in the center of the screen
or at specific xy-coordinates?

Thanks,
Zach.
 
Z

Zach

MC said:
The main menu strip of a window shows up at the top left.

A context menu strip shows up when the user right-clicks (e.g., in a
textbox or the like), and it shows up at the cursor position.

Can you tell us a little more about what you're wanting to do? There is
doubtless a way to deceive the context menu about where it should pop up,
but that's an odd way to do a menu.
oooooooooooooooooooooooooooooooooooooooooooooooooooooo

I show a menu strip on screen, at start-up, like so:
contextMenuStrip1.Show();
but it shows up in the top left hand corner.


So I am talking about a context menu strip
------------------------------------------

Question, how do I get it to show in the center of the screen
or at specific xy-coordinates?

Thanks,
Zach
 
J

Jeff Johnson

I show a menu strip on screen, at start-up, like so:
contextMenuStrip1.Show();
but it shows up in the top left hand corner.

Question, how do I get it to show in the center of the screen
or at specific xy-coordinates?

Assuming you're really talking about a ContextMenuSTRIP and not the older
ContextMenu, just use one of the overloads to Show() that takes a Point or a
pair of integers for x and y. The older ContextMenu also takes a Point
argument.
 
Z

Zach

Jeff Johnson said:
Assuming you're really talking about a ContextMenuSTRIP and not the older
ContextMenu, just use one of the overloads to Show() that takes a Point or
a pair of integers for x and y. The older ContextMenu also takes a Point
argument.
ooooooooooooooooooooooooooooooooooooooooooooooooooooo
Yes context menu strip!

Yes I discovered Show(x,y). So that problem is fixed.

What I am unable to do now is to get the context menu strip to appear in the
same place all the time

this.listView1.MouseDown += new MouseEventHandler(listView1_MouseDown);

void listView1_MouseDown(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Right)contextMenuStrip1.Show(100,
100);
}

This will get the pop up menu to show at 100,100 but when I let go of the
mouse button, the pop up menu disappears and reappears where the cursor is.
That reappearing stuff I cannot get rid of. Any idea? (I did also do a
mouseUp, but that didn't help).

Zach.
 
J

Jeff Johnson

Yes I discovered Show(x,y). So that problem is fixed.

What I am unable to do now is to get the context menu strip to appear in
the same place all the time

this.listView1.MouseDown += new MouseEventHandler(listView1_MouseDown);

void listView1_MouseDown(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Right)contextMenuStrip1.Show(100,
100);
}

This will get the pop up menu to show at 100,100 but when I let go of the
mouse button, the pop up menu disappears and reappears where the cursor
is.
That reappearing stuff I cannot get rid of. Any idea? (I did also do a
mouseUp, but that didn't help).

The Control object (and therefore everything derived from it) has a
ContextMenu property. Perhaps you've assigned your context menu to that
property and therefore it's getting displayed twice, once by your code and
then automatically by Windows Forms.
 
Z

Zach

Jeff Johnson said:
The Control object (and therefore everything derived from it) has a
ContextMenu property. Perhaps you've assigned your context menu to that
property and therefore it's getting displayed twice, once by your code and
then automatically by Windows Forms.
ooooooooooooooooooooooooooooooooooooooooooooooooooo
So what should I do?

Zach
 
Z

Zach

Jeff Johnson said:
...don't assign it to the ContextMenu property.
ooooooooooooooooooooooooooooooooooooooooooooooooooo

this.listView1.MouseDown += new MouseEventHandler(listView1_MouseDown);

void listView1_MouseDown(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Right)contextMenuStrip1.Show(100, 100);
}

This is what I have done. As far as I understand you, I have not assigned
the mouse action to the pop up menu. Or am I misunderstanding. If so, could
you help me a little bit more explicitly? Many thanks.

Zach.
 
Z

Zach

ooooooooooooooooooooooooooooooooooooooooooooo
When I remove all the "this." there is no change.
Zach.
 
J

Jeff Johnson

this.listView1.MouseDown += new MouseEventHandler(listView1_MouseDown);

Argh. Please oh please, NEVER display a popup menu on mouse DOWN. Right now,
go to any app on your system and click the right mouse button and hold it.
Does a menu pop up? I doubt it. Now let go. Menu, right? Please stick to
this standard.
if(e.Button == MouseButtons.Right)contextMenuStrip1.Show(100, 100);

Are you aware that those are SCREEN coordinates and therefore have nothing
to do with where the active window currently is? If your window is near the
top-left of the screen then the context menu will be close to the point
where your mouse was clicked (mostly). If it's to the right or down then the
menu will appear to be "way up left." Look at the PointToScreen() method of
the Control class for a solution. Call this on the list view (it's derived
from Control, ultimately, so it will have this method too).
 
Z

Zach

Jeff Johnson said:
Argh. Please oh please, NEVER display a popup menu on mouse DOWN. Right
now, go to any app on your system and click the right mouse button and
hold it. Does a menu pop up? I doubt it. Now let go. Menu, right? Please
stick to this standard.


Are you aware that those are SCREEN coordinates and therefore have nothing
to do with where the active window currently is? If your window is near
the top-left of the screen then the context menu will be close to the
point where your mouse was clicked (mostly). If it's to the right or down
then the menu will appear to be "way up left." Look at the PointToScreen()
method of the Control class for a solution. Call this on the list view
(it's derived from Control, ultimately, so it will have this method too).
ooooooooooooooooooooooooooooooooooooooooooooooooo
I tried both mouse down and mouse up.
However, your point is well taken.
I am aware of the fact that 100,100 are coordinates.
"PointToScreen() method" is new to me, for which thanks.
How to use the method in conjunction with a context menu strip is a puzzle
to me.
Might you lift the veil just a wee bit for me to have a peek?

Regards,
Zach.
 
J

Jeff Johnson

I am aware of the fact that 100,100 are coordinates.

I didn't say they are coordinates, I said they are "SCREEN coordinates," and
that means something very specific in Windows. An example is in order. Let's
make things easy and assume you just have one monitor.

The pixel at the very top left of your screen is screen coordinate (0, 0).
As you go right, the X values increase. As you go down, the Y values
increase. I mention this only because people with a heavy math background
may expect Y to increase as you go UP, but in Windows this is not the case
unless you explicitly tell Windows to do it this way, and that doesn't apply
to this example.

Now let's say you have Notepad open and that it's not maximized and is
located somewhere "middle-ish" on the screen. Look at the rectangle that it
occupies. Let's say the top-left pixel of Notepad's border is at the
physical screen location of (200, 100). Now look at the various pieces of
the Notepad window. There's the title bar, the menu bar, a big text box, and
surrounding the whole thing, a sizing border. Everything except the text box
is part of what is called the "non-client area" of the window, and this is
an area that your code generally doesn't touch (as far as drawing or placing
controls goes). The area occupied by the text box (i.e., everything that's
not the non-client area) is--surprise--the client area. This is where most
of the "action" occurs, that is to say, this is the area of the window that
your program can interact with the easiest. Because it is so common for a
program to interact with this area, Windows allows you to specify
coordinates that are local to this area and act as if it were the only area
on the screen. Therefore, the top-left corner of this area has a CLIENT
coordinate of (0, 0), just as if it were at the top-left of your monitor.

But it's not. Unless you have your window positioned in a really weird way,
the client coordinate (0, 0) almost never represents the same physical point
as screen coordinate (0, 0). In our case, this client coordinate may be
physical point (205, 125). So if you right-click in this area and your
program wants to display a context menu at client coordinate (0, 0), but the
context menu only deals in screen coordinates, you've got to translate your
relative (logical) client coordinate to an absolute (physical) screen
coordinate. That's what PointToScreen() does. Windows knows where your
form's client area is at all times and can turn any client point into a
screen point for those things that need screen points to operate.
"PointToScreen() method" is new to me, for which thanks.
How to use the method in conjunction with a context menu strip is a
puzzle to me.
Might you lift the veil just a wee bit for me to have a peek?

I'm much more a believer in teaching a man to fish instead of giving him a
fish, so with what I told you above, see if you can't figure it out yourself
first.
 
Z

Zach

Jeff Johnson said:

I have written a number of applications that place forms and graphs on the
screen using coordinates, so I don't think coordinates are bothering me
terribly.
I'm much more a believer in teaching a man to fish instead of giving him a
fish, so with what I told you above, see if you can't figure it out
yourself first.
I have obviously spent much time in trying to solve my query myself before
posting a question here and have no stomach for neurotic game playing.

Zach.
 
J

Jeff Johnson

I have written a number of applications that place forms and graphs on the
screen using coordinates, so I don't think coordinates are bothering me
terribly.
I have obviously spent much time in trying to solve my query myself before
posting a question here and have no stomach for neurotic game playing.

Well, so much for trying to help.

Given your responses, I had absolutely no idea whether or not you knew
anything about this. I got the impression that you thought I was only trying
to tell you what "coordinates" were and didn't get that there was a
distinction between screen coordinates and client coordinates. I still find
it rather hard to believe that, given the experience you claim to have,
you're having such difficulty in the concept of translating client
coordinates to screen coordinates.

Maybe I've gone off on a tangent here and we need to take a step back.
Perhaps when you posted the code

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

you were just being generic. I, however, believed that this was the ACTUAL
code in your application, and this took me down the screen vs. client path.
Is it your actual code? Is it not? As Peter is fond of saying, please post a
concise-but-complete code sample so that we can see exactly what you're
doing and exactly what's happening. Telling us which version of the .NET
Framework you're using would also be helpful.
 
Z

Zach

Dear Jeff,

I wasn't trying to be disrespectful, however, I have been on this problem
for many hours now and you and I do not seem to be getting anywhere. My
problem was clearly stated.
this.listView1.MouseDown += new MouseEventHandler(listView1_MouseDown);

void listView1_MouseDown(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Right)contextMenuStrip1.Show(100,
100);
}

This will get the ... menu to show at 100,100 but when I let go of the
mouse button, the .... menu disappears and reappears where the cursor is.

Obviously I only want the menu diaplayed at 100, 100 (e.g.) and not a second
time where the cursor is.

Many thanks anyway.
Regards,
Zach.
 
J

Jeff Johnson

I wasn't trying to be disrespectful, however, I have been on this problem
for many hours now and you and I do not seem to be getting anywhere. My
problem was clearly stated.


Obviously I only want the menu diaplayed at 100, 100 (e.g.) and not a
second time where the cursor is.

Before I potentially waste time testing this in the wrong environment, I
will ask again: WHAT VERSION of the .NET Framework are you writing this
program under?
 
Z

Zach

Jeff Johnson said:
Before I potentially waste time testing this in the wrong environment, I
will ask again: WHAT VERSION of the .NET Framework are you writing this
program under?
ooooooooooooooooooooooooooooooooooooooooooooooooooooo
I develop on Vista Business. I use Studio 2005.
That box has V1, V2 and V3.0 on board.

Zach.
 
Z

Zach

Jeff Johnson said:
Before I potentially waste time testing this in the wrong environment, I
will ask again: WHAT VERSION of the .NET Framework are you writing this
program under?
**************************************************
MouseUp and MouseDown can both be made to display
a contexrMenuStrip on a simple form, without an echo, by
which I mean that if MouseDown creates a menu, releasing
the mouse button does not create another menu, unless
there is code to that effect.

On a list view that is different. If you create a menu on
Mouse Down, another menu will be cteated without
asking for it, at MouseUp.

Zach.
 
J

Jeff Johnson

I develop on Vista Business. I use Studio 2005.
That box has V1, V2 and V3.0 on board.

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.
 
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
I could replicate a result you get on an XP box as well.
Zach.
 

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