context menus

C

Charles Crawford

Hello,

I'm writing a windows forms application and have a custom user control built
from multiple controls, including a picturebox control onto which I'm
drawing a clock face. Each hour on the clock face represents a 'node' and I
have the application switch cursors from the arrow to hand cursors each time
the mouse moves over one of the nodes.

I have a contextmenustrip control assigned to the picturebox control, but I
only wish to display the context menu when the mouse is over one of the
nodes.

I've tried to use the mousedown event on the picturebox as follows:

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right && pictureBox1.Cursor ==
Cursors.Hand)
{
pictureBox1.Cursor = Cursors.Arrow;
contextMenuStrip1.Visible = true;
}
else
{
contextMenuStrip1.Visible = false;
}
}

The problem I'm having is that the context menu shows up whenever I
right-click on the picturebox, regardless of whether it's over a 'node' or
not.

Thanks in advance.

Charlie
 
J

JJ

Without knowledge of the internal of your node, below is the best I can come
up with.

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right && InsideOfMyNode()) // You must
define InsideOfMyNode()
{
pictureBox1.Cursor = Cursors.Arrow;
contextMenuStrip1.Visible = true;
}
else
{
contextMenuStrip1.Visible = false;
}
}

Good luck
Jeff
 
L

Linda Liu [MSFT]

Hi Charles,

Thank you for posting here!

I notice that you have posted the same question in our
microsoft.public.dotnet.framework.windowforms.controls newsgroup, to which
I have already responded. So please check my answer there and if you need
any further assistance on this particular issue, please reply to me in that
thread, so I can follow up with you in time.

For your convenience, I have included my reply as follows:

Have you set the ContextMenuStrip property of the PictureBox on your
UserControl to the contextMenuStrip1? If so, the context menu shows up
whenever you right-click on the picturebox, regardless of whether it is
over a "node" or not. I recommend you to clear the ContextMenuStrip
property of the PictureBox.

In addition, I think you may modify your code as follows to ensure the
contextMenuStrip1 to show up at the location of the Mouse.

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right && pictureBox1.Cursor
==Cursors.Hand)
{
pictureBox1.Cursor = Cursors.Arrow;
// contextMenuStrip1.Visible = true;
contextMenuStrip1.Show(this.pictureBox1,e.Location);
}
// comment out the code below
// else
// {
// contextMenuStrip1.Visible = false;
// }
}

Have a nice day!


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

Charles Crawford

Thanks Linda,

This is EXACTLY what I was looking for.

Just out of curiosity, how is the average VStudio/C# programmer supposed to
stumble across this information when all of the example code and
documentation provided by Microsoft only address the most simplistic usage
of controls and classes? There also seems to be an over abundance of web
sites that simply duplicate Microsoft's documentation on their own sites.

Thank goodness folks like you exist that can provide us with quick and
meaningful responses.

Thanks again,

Charlie
 

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