C#如何实现动态添加右键菜单

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#如何实现动态添加右键菜单相关的知识,希望对你有一定的参考价值。

RT,我用了ContextMenuStrip 控件,请问用什么方法实现我生成的EXE能够动态的添加右键菜单,具体的就是我要用一个方法实现单击打开Windows自带的浏览窗口,然后生成一个文件路径,这个文件路径保存在一个变量中,然后我要在右键菜单中生成一个与之对应的键,使得单击可以打开那个文件路径对应的文件~
在线急等啊~~
解决了我在追加分~~~
这个问题我已经解决了,多谢回答,我现在碰到的问题是需要用数据库保存已经生成的文件路径和文件名,然后在打开程序的时候能生成对应的ToolStripMenuItem,请问这个怎么实现?具体点来说就是把数据库里面存的所有文件路径和文件名生成为ToolStripMenuItem 另外,我用的是acess数据库,表里面只需要有文件路径和文件名就行了

参考技术A 这个貌似不难,右键菜单用代码生成就行了。
你是要用ContextMenuStrip控件,生成的菜单可以用代码控制。如
// This code example demonstrates how to handle the Opening event.
// It also demonstrates dynamic item addition and dynamic
// SourceControl determination with reuse.
class Form3 : Form

// Declare the ContextMenuStrip control.
private ContextMenuStrip fruitContextMenuStrip;

public Form3()

// Create a new ContextMenuStrip control.
fruitContextMenuStrip = new ContextMenuStrip();

// Attach an event handler for the
// ContextMenuStrip control's Opening event.
fruitContextMenuStrip.Opening += new System.ComponentModel.CancelEventHandler(cms_Opening);

// Create a new ToolStrip control.
ToolStrip ts = new ToolStrip();

// Create a ToolStripDropDownButton control and add it
// to the ToolStrip control's Items collections.
ToolStripDropDownButton fruitToolStripDropDownButton = new ToolStripDropDownButton("Fruit", null, null, "Fruit");
ts.Items.Add(fruitToolStripDropDownButton);

// Dock the ToolStrip control to the top of the form.
ts.Dock = DockStyle.Top;

// Assign the ContextMenuStrip control as the
// ToolStripDropDownButton control's DropDown menu.
fruitToolStripDropDownButton.DropDown = fruitContextMenuStrip;

// Create a new MenuStrip control and add a ToolStripMenuItem.
MenuStrip ms = new MenuStrip();
ToolStripMenuItem fruitToolStripMenuItem = new ToolStripMenuItem("Fruit", null, null, "Fruit");
ms.Items.Add(fruitToolStripMenuItem);

// Dock the MenuStrip control to the top of the form.
ms.Dock = DockStyle.Top;

// Assign the MenuStrip control as the
// ToolStripMenuItem's DropDown menu.
fruitToolStripMenuItem.DropDown = fruitContextMenuStrip;

// Assign the ContextMenuStrip to the form's
// ContextMenuStrip property.
this.ContextMenuStrip = fruitContextMenuStrip;

// Add the ToolStrip control to the Controls collection.
this.Controls.Add(ts);

//Add a button to the form and assign its ContextMenuStrip.
Button b = new Button();
b.Location = new System.Drawing.Point(60, 60);
this.Controls.Add(b);
b.ContextMenuStrip = fruitContextMenuStrip;

// Add the MenuStrip control last.
// This is important for correct placement in the z-order.
this.Controls.Add(ms);


// This event handler is invoked when the ContextMenuStrip
// control's Opening event is raised. It demonstrates
// dynamic item addition and dynamic SourceControl
// determination with reuse.
void cms_Opening(object sender, System.ComponentModel.CancelEventArgs e)

// Acquire references to the owning control and item.
Control c = fruitContextMenuStrip.SourceControl as Control;
ToolStripDropDownItem tsi = fruitContextMenuStrip.OwnerItem as ToolStripDropDownItem;

// Clear the ContextMenuStrip control's Items collection.
fruitContextMenuStrip.Items.Clear();

// Check the source control first.
if (c != null)

// Add custom item (Form)
fruitContextMenuStrip.Items.Add("Source: " + c.GetType().ToString());

else if (tsi != null)

// Add custom item (ToolStripDropDownButton or ToolStripMenuItem)
fruitContextMenuStrip.Items.Add("Source: " + tsi.GetType().ToString());


// Populate the ContextMenuStrip control with its default items.
fruitContextMenuStrip.Items.Add("-");
fruitContextMenuStrip.Items.Add("Apples");
fruitContextMenuStrip.Items.Add("Oranges");
fruitContextMenuStrip.Items.Add("Pears");

// Set Cancel to false.
// It is optimized to true based on empty entry.
e.Cancel = false;

本回答被提问者和网友采纳

C#上位机开发—— 右键菜单的使用

一、菜单控件的基本使用

1. ListBox

放置一个ListBox,用于右键菜单操作,并默认添加5个数据项:

2. 添加菜单控件

添加右键菜单控件:

3. 为每个菜单项添加名称

点击右键菜单控制中的【添加】菜单项,然后在属性中找到Name,修改名称:

同样,修改其它两个菜单项的名称。

二、设置为右键菜单

1. 添加鼠标单击事件

给ListBox添加鼠标事件MouseUp:

2. 弹出菜单

在鼠标单击事件回调函数中,弹出菜单:

private void listBox1_MouseUp(object sender, MouseEventArgs e)

    if (e.Button == MouseButtons.Right)
    
        contextMenuStrip1.Show(listBox1, e.Location);
    
    

运行,效果如下:

3. 根据鼠标点击位置区分不同功能

private void listBox1_MouseUp(object sender, MouseEventArgs e)

    // 是否右键单击
    if (e.Button == MouseButtons.Right)
    
        // 是否在空白处单击
        int index = listBox1.IndexFromPoint(e.Location);
        if (index >= 0)
        
            // 选中该项
            listBox1.SetSelected(index, true);

            // 开启修改、添加、删除功能
            ModifyToolStripMenuItem.Enabled = true;
            DeleteToolStripMenuItem.Enabled = true;
        
        else
        
            // 清空选择
            listBox1.ClearSelected();

            // 空白处单击,只显示添加
            ModifyToolStripMenuItem.Enabled = false;
            DeleteToolStripMenuItem.Enabled = false;
        


        contextMenuStrip1.Show(listBox1, e.Location);
    

运行,看看效果:

三、右键菜单项的回调

直接双击该菜单项:

自动添加回调函数:

private void AddToolStripMenuItem_Click(object sender, EventArgs e)

    listBox1.Items.Add("new student");

看看效果:

以上是关于C#如何实现动态添加右键菜单的主要内容,如果未能解决你的问题,请参考以下文章

C# winform datagridview中如何实现鼠标右键点击一行数据出现一个带有删除的菜单,并能执行删除操作?

c# 怎么代码创建右键菜单的快捷键?

DataGridview加上右键菜单 .net 2005 c#

C#上位机开发—— 右键菜单的使用

C#上位机开发—— 右键菜单的使用

C#上位机开发—— 右键菜单的使用