Click to See Complete Forum and Search --> : Context Menu strip - Top left


gborges
November 24th, 2008, 12:14 PM
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:

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:


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!

BigEd781
November 24th, 2008, 03:22 PM
The Show method of a context menu is overloaded and optionally takes a point as an argument. Set this to the mouse coordinates.


protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.button == MouseButtons.Right)
{
this.ticksMenuStrip.Show(e.Location);
}
}

ZOverLord
December 31st, 2008, 04:38 PM
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.



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();


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