为什么在第一秒使用contextmenustrip右键单击时菜单不在鼠标位置?

时间:2017-10-21 08:11:41

标签: c# .net winforms

在构造函数中:

ListViewCostumControl.lvnf.ContextMenuStrip = contextMenuStrip1;
ToolStripMenuItem item1 = new ToolStripMenuItem();
item1.Name = "Open File Folder Location";
item1.Text = "Open File Folder Location";
item1.Click += contextMenuStrip1_ItemClick;
ToolStripMenuItem item2 = new ToolStripMenuItem();
item2.Name = "Launch File";
item2.Text = "Launch File";
item2.Click += contextMenuStrip2_ItemClick;
ToolStripMenuItem item3 = new ToolStripMenuItem();
item3.Name = "Copy File";
item3.Text = "Copy File";
item3.Click += contextMenuStrip3_ItemClick;
contextMenuStrip1.Items.Add(item1);
contextMenuStrip1.Items.Add(item2);
contextMenuStrip1.Items.Add(item3);

然后我检查listView是否仅为空,然后显示菜单:

private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            if (ListViewCostumControl.lvnf.Items.Count == 0)
            {
                e.Cancel = true;
            }
        }

当我右键单击以显示菜单时的事件:

private void Lvnf_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right && ListViewCostumControl.lvnf.Items.Count > 0)
            {
                var hitTestInfo = ListViewCostumControl.lvnf.HitTest(e.X, e.Y);
                if (hitTestInfo.Item != null)
                {
                    var loc = e.Location;
                    loc.Offset(ListViewCostumControl.lvnf.Location);

                    var items = ListViewCostumControl.lvnf.CheckedItems;
                    if (items.Count > 0)
                    {
                        contextMenuStrip1.Items[2].Enabled = true;
                    }
                    else
                    {
                        contextMenuStrip1.Items[2].Enabled = false;
                    }
                    contextMenuStrip1.Show(this, loc);
                }
            }
        }

当我鼠标右键单击一秒或更短时间时,contextmenustrip的菜单显示在form1的顶部,然后它显示在listView上鼠标所在的位置选择项目。

但我怎么能让它只在项目上显示菜单?

我从toolbox拖动了设计器中的contextMenuStrip1,我现在看到contextmenustrip也显示为form1左上角的菜单。

这是在我右键单击表单左上角后立即显示菜单的屏幕截图:

First

这是第二次,菜单第一次移动到应该的位置:

First1

1 个答案:

答案 0 :(得分:1)

问题在于这一行:

To change the port:-
1) open iis
2) select your project 
3) there is a "Edit Bindings" option at the right side. click on it.
4) select the host and click on edit.
5) now change the port

这更喜欢form1控件,它应该是listView控件:

contextMenuStrip1.Show(this, loc);