ContextMenuStrip

  • Thread starter Thread starter Alberto
  • Start date Start date
A

Alberto

Can I change by code the place where a ContextMenuStrip is placed when it's
opened?
Thank you
 
Can I change by code the place where a ContextMenuStrip is placed when
it's opened?

You could try adding an event handler to the Opening event and then changing
the control's Location property. I haven't tested it, though.
 
I tried what you said but it doesn't work. First, in the mouse down event
handler, I get the e.x and e.y (the position of the pointer) and in the
event handler of the opening event, I change the ContexMenuStrip.Bound
property as this:

menu.Bound = new Rectangle(x, y, menu.Bound.Width, menu.Bound.Height);

The problem is that the context menu doesn't appear where the pointer is.
 
I tried what you said but it doesn't work. First, in the mouse down event
handler, I get the e.x and e.y (the position of the pointer) and in the
event handler of the opening event, I change the ContexMenuStrip.Bound
property as this:

menu.Bound = new Rectangle(x, y, menu.Bound.Width, menu.Bound.Height);

Those might be window coordinates and the menu may expect screen coordinates
(or vice-versa). Somewhere there is a ClientToScreen() method or something
similar. I'd look it up for you, but I'm in the process of shutting this
machine down....
 
Alberto said:
I tried what you said but it doesn't work. First, in the mouse down
event handler, I get the e.x and e.y (the position of the pointer) and
in the event handler of the opening event, I change the
ContexMenuStrip.Bound property as this:

menu.Bound = new Rectangle(x, y, menu.Bound.Width, menu.Bound.Height);

The problem is that the context menu doesn't appear where the pointer is.

The context menu should appear at the mouse pointer by default. If it
doesn't, the way to fix that is to figure out why the default behavior
isn't happening in your case, rather than adding yet more code.

As far as the more general question goes, you can't change the Bounds
property in the Opening event and move the ContextMenuStrip, but you can
modify the Bounds in the Opened event and accomplish that. I've tested
it, and it works fine.

Pete
 
I agree with you that I should try to find out why the ContextMenu isn't
appearing in the default position but I don't know how to do it.

The situation is this: I have a control inherited from Panel who works as a
container but it's specialized with some methods and properties. In this
container I have controls inherited from Label who have a ContexMenuStrip
who are called "Nodo".

The first time I press the right button of the mouse over a "Nodo" control,
the menu appear with right x coordinate but with a wrong coordinate Y (y=0).
The sencond and next times I press the right button, the menu appears in the
right position.

It's very strange and I don't know how to trace it.
 
Alberto said:
I agree with you that I should try to find out why the ContextMenu isn't
appearing in the default position but I don't know how to do it.

The situation is this: I have a control inherited from Panel who works
as a container but it's specialized with some methods and properties. In
this container I have controls inherited from Label who have a
ContexMenuStrip who are called "Nodo".

The first time I press the right button of the mouse over a "Nodo"
control, the menu appear with right x coordinate but with a wrong
coordinate Y (y=0). The sencond and next times I press the right button,
the menu appears in the right position.

It's very strange and I don't know how to trace it.

As always, your first step is to create a code example that minimally
reproduces the problem. Either start from a very simple example that
works fine, and add elements similar to your own program until it
breaks. Or start with your current code and simplify it until it starts
working again.

Either way, at some point you'll have a transition between working code
and non-working code that provides a strong indicator as to what about
your code might be causing the problem.

If it turns out that you can reproduce the problem even with the
simplest of examples, without your own code doing anything special, then
you may have found a bug in .NET. But until you've done the work to
make that "simplest example", you can't say that for sure.

Pete
 
Alberto said:
I agree with you that I should try to find out why the ContextMenu isn't
appearing in the default position but I don't know how to do it.

The situation is this: I have a control inherited from Panel who works
as a container but it's specialized with some methods and properties. In
this container I have controls inherited from Label who have a
ContexMenuStrip who are called "Nodo".

The first time I press the right button of the mouse over a "Nodo"
control, the menu appear with right x coordinate but with a wrong
coordinate Y (y=0). The sencond and next times I press the right button,
the menu appears in the right position.

Hi Alberto,

I had tried to re-produce the problem based on your above description.
But no, the ContextMenuStrip works correctly as expected. It appear
exactly where the mouse pointer is.

You may need to post the code so others may try to help you.

Regards.
 
Back
Top