How to create a context menu in C#

守給你的承諾、 提交于 2019-12-02 00:48:40

问题


I want to create a context menu using C# that will display next to the node similar to what happens here in 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;
        }
    }            
}

回答1:


Why not simply use Form.ShowDialog?

From MSDN:

Form.ShowDialog Method

Shows the form as a modal dialog box.




回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/10821835/how-to-create-a-context-menu-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!