Reputation: 11341
In the constructor:
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);
Then i check that the listView
is not empty only then show the menu:
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
if (ListViewCostumControl.lvnf.Items.Count == 0)
{
e.Cancel = true;
}
}
And the event when i make right click to show the menu:
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);
}
}
}
When i make mouse right click for a second or less the menu of the contextmenustrip
is show on the top of the form1
then it's showing on the listView where the mouse is on the select item.
But how can i make that it will show the menu only on the item ?
I dragged the contextMenuStrip1 in the designer from the toolbox
and i see now that the contextmenustrip
is also show as menu in the top left of the form1
.
This is a screenshot showing the menu right after i make right click on the top left of the form:
And this is second later the menu is moving to where it should be on the first time:
Upvotes: 2
Views: 311
Reputation: 11341
The problem was this line:
contextMenuStrip1.Show(this, loc);
this prefer to form1 control and it should be the listView control instead:
contextMenuStrip1.Show(ListViewCostumControl.lvnf, loc);
Upvotes: 2