示例#1
0
        private void ListBoxItemsMouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Right)
            {
                return;
            }
            var loc = ListBoxItems.PointToScreen(new Point(e.X, e.Y));

            for (int i = 0; i < ListBoxItems.Items.Count; i++)
            {
                var rec = ListBoxItems.GetItemRectangle(i);
                if (rec.X <= e.X && (rec.X + rec.Width) >= e.X && rec.Y <= e.Y && (rec.Y + rec.Height) >= e.Y)
                {
                    ItemContextMenu.Items[0].Enabled = true;
                    ItemContextMenu.Items[2].Enabled = true;
                    ItemContextMenu.Items[3].Enabled = true;
                    ItemContextMenu.Items[4].Enabled = true;
                    ListBoxItems.ClearSelected();
                    ListBoxItems.SetSelected(i, true);
                    ItemContextMenu.Show(loc);
                    return;
                }
            }
            ItemContextMenu.Items[0].Enabled = false;
            ItemContextMenu.Items[3].Enabled = false;
            ItemContextMenu.Items[4].Enabled = false;
            ItemContextMenu.Show(loc);
        }