当我单击鼠标左键C#时如何显示上下文菜单

时间:2018-09-30 14:11:18

标签: c# winforms

我尝试了此代码,但没有用,请帮助

if(e.Buttons == MouseButton.left){
    contextMenu Menu_one = new contextMenu();
    var menu_item_copy = new MenuItem("Copy", menu_item_copy_click)
    Menu_one.Menuitem.add(menu_item_copy );
    button_one.contextMenu = Menu_one;
}

3 个答案:

答案 0 :(得分:0)

if(e.Buttons == MouseButton.left){
    contextMenu Menu_one = new contextMenu();
    var menu_item_copy = new MenuItem("Copy", menu_item_copy_click)
    Menu_one.Menuitem.add(menu_item_copy );
    button_one.contextMenu = Menu_one;
    Menu_one.Show();
}

这是您忘记的那一行:   Menu_one.Show();

答案 1 :(得分:0)

private void button_one_MouseClick(object sender, MouseEventArgs e)
{
    if (e.Button is MouseButtons.Left)
    {
        button_one.ContextMenu = new ContextMenu();
        button_one.ContextMenu.Add("Copy", menu_item_copy_click);

        button_one.ContextMenu.Show(button_one, new Point(e.X, e.Y));
    }
}

private void menu_item_copy_click(object sender, EventArgs e)
{
    // Do something here
}

答案 2 :(得分:0)

 button_one.ContextMenu.Show(button_one, new Point(e.X, e.Y), LeftRightAlignment.Right);