-
November 24th, 2008, 01:14 PM
#1
Context Menu strip - Top left
Hi all!
I am in trouble trying to positioning a context menu strip.
I will explain my application, here.
There is a usercontrol, which the user can right click using mouse and so, I would like that a customized menu appers, just after this action.
I create a context menu, using this code:
Code:
private void InitializeContextMenu() {
this.components = new System.ComponentModel.Container();
this.ticksMenuStrip = new System.Windows.Forms.ContextMenuStrip(this.components);
this.ticksToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.tenthMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.seventhMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.ticksMenuStrip.SuspendLayout();
this.SuspendLayout();
//
// ticksMenuStrip
//
this.ticksMenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.ticksToolStripMenuItem});
this.ticksMenuStrip.Name = "ticksMenuStrip";
this.ticksMenuStrip.Size = new System.Drawing.Size(153, 48);
//
// ticksToolStripMenuItem
//
this.ticksToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.tenthMenuItem,
this.seventhMenuItem});
this.ticksToolStripMenuItem.Name = "ticksToolStripMenuItem";
this.ticksToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.ticksToolStripMenuItem.Text = "Ticks";
//
// tenthMenuItem
//
this.tenthMenuItem.Name = "tenthMenuItem";
this.tenthMenuItem.Size = new System.Drawing.Size(152, 22);
this.tenthMenuItem.Text = "1/10 of scale";
this.tenthMenuItem.Click += new System.EventHandler(this.tenthMenuItem_Click);
//
// seventhMenuItem
//
this.seventhMenuItem.Name = "seventhMenuItem";
this.seventhMenuItem.Size = new System.Drawing.Size(152, 22);
this.seventhMenuItem.Text = "1/7 of scale";
this.seventhMenuItem.Click += new System.EventHandler(this.seventhMenuItem_Click);
//
//
//
this.ticksMenuStrip.ResumeLayout(false);
this.ResumeLayout(false);
}
I get the mouse right click using this code:
Code:
protected override void controlEx_MouseDown(Object sender, MouseEventArgs e) {
switch (e.Button) {
case MouseButtons.Right:
this.ticksMenuStrip.Show();
break;
}
return;
}
The problem is that when I right-click over the user control the context menu appears on top left position of the screen, but I would like that it appears just where I click using the mouse.
I read that I would add a notifyIcon in order to do it, but it also does no works.
Could someone help me?
Thank you in advance!
-
November 24th, 2008, 04:22 PM
#2
Re: Context Menu strip - Top left
The Show method of a context menu is overloaded and optionally takes a point as an argument. Set this to the mouse coordinates.
Code:
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.button == MouseButtons.Right)
{
this.ticksMenuStrip.Show(e.Location);
}
}
Last edited by BigEd781; November 24th, 2008 at 04:24 PM.
-
December 31st, 2008, 05:38 PM
#3
Re: Context Menu strip - Top left
This "looks" better because the ContextMenuStrip displays where the persons mouse event was captured, NOT the location of a control, which in some cases, could be very large. Example: Form1.
Your statement of:
"but I would like that it appears just where I click using the mouse." means that this is your best choice.
Code:
protected override void controlEx_MouseDown(Object sender, MouseEventArgs e) {
switch (e.Button) {
case MouseButtons.Right:
this.ticksMenuStrip.Show(Control.MousePosition);
break;
}
return;
}
This is the line vs what you had of: this.ticksMenuStrip.Show();
Code:
protected override void controlEx_MouseDown(Object sender, MouseEventArgs e) {
switch (e.Button) {
case MouseButtons.Right:
this.ticksMenuStrip.Show();
break;
}
return;
}
This shows many ways to deal with ContextMenus and NotifyIcon and more, and can be found here: http://code.msdn.microsoft.com/TheNotifyIconExample
Last edited by ZOverLord; December 31st, 2008 at 05:56 PM.
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
|