如何创建在C#中右键单击事件处理程序 [英] How to create a right click event handler in c#
本文介绍了如何创建在C#中右键单击事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想请想知道如何显示上下文菜单当你右击该窗口
下面是我到目前为止的代码:
私人无效ShowContextMenu_RightClick(对象发件人,EventArgs五)
{
toolStripMenuItem5.Visible = TRUE;
}
私人无效toolStripMenuItem5_Click(对象发件人,EventArgs五)
{
MessageBox.Show(您好,这是我的第三个应用程序,它是*动画*,编程由D&放大器; K);
}
解决方案
在形式CS文件,您可以附上你这样的上下文菜单。
公共Form1中()
{
的InitializeComponent();
//创建右键菜单..
的ContextMenuStrip S =新的ContextMenuStrip();
//添加一个名为为hello
ToolStripMenuItem你好=新ToolStripMenuItem()一个右键菜单项;
hello.Text =你好;
//添加招呼项目$ B $的clickevent b hello.Click + = hello_Click;
//在右键菜单
s.Items.Add添加项目(你好);
//重视与形式
右键菜单this.ContextMenuStrip = S;
}
无效hello_Click(对象发件人,EventArgs五)
{
MessageBox.Show(你好单击的);
}
I would please like to know how to display a context menu when you right click on the window.
Here is my code so far :
private void ShowContextMenu_RightClick(object sender, EventArgs e)
{
toolStripMenuItem5.Visible = true;
}
private void toolStripMenuItem5_Click(object sender, EventArgs e)
{
MessageBox.Show("Hi there this is my 3rd app which is *animation*.", "Programmed by D & K");
}
解决方案
in form cs file you can attach your context menu like this..
public Form1()
{
InitializeComponent();
//Create right click menu..
ContextMenuStrip s = new ContextMenuStrip();
// add one right click menu item named as hello
ToolStripMenuItem hello = new ToolStripMenuItem();
hello.Text = "Hello";
// add the clickevent of hello item
hello.Click += hello_Click;
// add the item in right click menu
s.Items.Add(hello);
// attach the right click menu with form
this.ContextMenuStrip = s;
}
void hello_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello Clicked");
}
这篇关于如何创建在C#中右键单击事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文