如何在 C# 中创建上下文菜单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10821835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:23:49  来源:igfitidea点击:

How to create a context menu in C#

c#winformsvisual-studio-2010contextmenu

提问by Antarr Byrd

I want to create a context menu using C# that will display next to the node similar to what happens here in Visual Studio:

我想使用 C# 创建一个上下文菜单,该菜单将显示在节点旁边,类似于 Visual Studio 中发生的情况:

The code I have now causes the main form to flicker.

我现在的代码导致主窗体闪烁。

private void treeView1_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        var myForm = new Form {Text = "My Form"};
        myForm.SetBounds(10, 10, 200, 200);

        myForm.Show();
        // Determine if the form is modal.
        if (myForm.Modal == false)
        {
            // Change borderstyle and make it not a top level window.
            myForm.FormBorderStyle = FormBorderStyle.FixedToolWindow;
            myForm.TopLevel = false;
        }
    }            
}

采纳答案by Bruno Brant

Why not simply use Form.ShowDialog?

为什么不简单地使用Form.ShowDialog

From MSDN:

MSDN

Form.ShowDialog Method

Form.ShowDialog 方法

Shows the form as a modal dialog box.

将窗体显示为模式对话框。

回答by Tim S.

You should set up treeView1.ContextMenuinstead of the approach you're taking.

你应该设置treeView1.ContextMenu而不是你正在采取的方法。

回答by Talha

You should read and try ContextMenucontrol of c#. I think it will resolve your problem rather than the technique you used..... Or other than use, myform.showdialog();with setbounds()methods.

您应该阅读并尝试ContextMenu控制 c#。我认为它将解决您的问题,而不是您使用的技术..... 或者除了使用之外,myform.showdialog();还有setbounds()方法。