|
-
May 22nd, 2009, 05:19 AM
#1
ContextMenuStrip showing on a left click
I want my button’s ContextMenuStrip to show when the user clicks on the left mouse button, and not on the right one.
So, in the MouseDown event of the button I do the following:
Code:
if (e.Button == MouseButtons.Right)
{
this.btn.ContextMenuStrip = null;
}
else if (e.Button == MouseButtons.Left)
{
this.btn.ContextMenuStrip = this.btnConMenuStrip;
this.btn.ContextMenuStrip.Show(this, PointToScreen(new Point(e.X, e.Y)));
}
The problem is that the ContextMenuStrip is not located where it’s supposed to be.
Am I doing something wrong?
Is there a better a way to achieve what I want?
-
May 22nd, 2009, 12:17 PM
#2
Re: ContextMenuStrip showing on a left click
Why are you calling PointToScreen? You don't need to, it will show at the correct client coordinate by doing this:
Code:
if (e.Button == MouseButtons.Right)
{
this.btn.ContextMenuStrip = null;
}
else if (e.Button == MouseButtons.Left)
{
this.btn.ContextMenuStrip = this.btnConMenuStrip;
this.btn.ContextMenuStrip.Show(this, e.Location);
}
-
May 22nd, 2009, 12:21 PM
#3
Re: ContextMenuStrip showing on a left click
 Originally Posted by Holly_vi
I want my button’s ContextMenuStrip to show when the user clicks on the left mouse button, and not on the right one.
So, in the MouseDown event of the button I do the following:
Code:
if (e.Button == MouseButtons.Right)
{
this.btn.ContextMenuStrip = null;
}
else if (e.Button == MouseButtons.Left)
{
this.btn.ContextMenuStrip = this.btnConMenuStrip;
this.btn.ContextMenuStrip.Show(this, PointToScreen(new Point(e.X, e.Y)));
}
The problem is that the ContextMenuStrip is not located where it’s supposed to be.
Am I doing something wrong?
Is there a better a way to achieve what I want?
Can you tell me why you transform the location using PointToScreen ?
e.X and e.Y is related to the form where your mouse is just over it
and the Context meue IMHO is also related to this coordinates.
I just looked into the MSDN data. There is only one method signature which uses screen cooredinates and no other input params all others are using client coordinates. Additional if you are looking to the buttons ContextmenueStrip Property you will read that it will be shown when you are rightclicking to that button. ALWAYS. The show method allows you to change the position where it is to be shown. And you are relating it to 'this' which is always that form where your control is located ( Assuming the showed code is part of the forms code and not in a separate datamodel class or anywhere else )
Anyway your code only asign this position when leftclicking the mouse, while the menue will be shown only when rightclicking, but at this time your code is never be reached. So how should this work ??
Another point will maybe start a discussion. Because IMHO users have the Right that programs work in a way they assume they will work. There was something like a standard created years ago and you should NOT try to do unusual things in your program. People dislike such 'jokes' when a program reacts in a different manner then they assume it should do- So best way is SIMPLE FORGET IT
Last edited by JonnyPoet; May 22nd, 2009 at 01:10 PM.
 Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|