C#上下文菜单(右键菜单)ContextMenuStrip用法总结
设置窗体的ContextMenuStrip属性或者也可以在按钮单击事件里调出:
url:http://greatverve.cnblogs.com/archive/2012/03/13/ContextMenuStrip.html
private void button_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
btn.FlatAppearance.BorderSize = 0;
Point point = new Point(btn.Left + this.Left + 5, btn.Top + this.Top + 40);
contextMenuStrip.Show(point);
}
{
Button btn = sender as Button;
btn.FlatAppearance.BorderSize = 0;
Point point = new Point(btn.Left + this.Left + 5, btn.Top + this.Top + 40);
contextMenuStrip.Show(point);
}
/// <summary>
/// 绑定右键菜单
/// </summary>
private void BindMenu()
{
ToolStripMenuItem menu0 = GetMenuItem("一级1", imageList1.Images[0]);
ToolStripMenuItem menu01 = GetMenuItem("二级11", imageList1.Images[1]);
menu01.Click += new EventHandler(toolStripMenuItem_Click);//添加事件
ToolStripMenuItem menu02 = GetMenuItem("二级12", imageList1.Images[2]);
menu0.DropDownItems.Add(menu01);
menu0.DropDownItems.Add(menu02);
cMenu.Items.Add(menu0);
ToolStripMenuItem menu1 = GetMenuItem("一级2", imageList1.Images[0]);
ToolStripMenuItem menu11 = GetMenuItem("二级21", imageList1.Images[1]);
ToolStripMenuItem menu12 = GetMenuItem("二级22", imageList1.Images[2]);
menu1.DropDownItems.Add(menu11);
menu1.DropDownItems.Add(menu12);
cMenu.Items.Add(menu1);
}
/// <summary>
/// 菜单项事件响应
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolStripMenuItem_Click(object sender, EventArgs e)
{
ToolStripMenuItem menu = sender as ToolStripMenuItem;
MessageBox.Show(menu.Text);
}
/// <summary>
/// 生成菜单项
/// </summary>
/// <param name="txt"></param>
/// <param name="img"></param>
/// <returns></returns>
private ToolStripMenuItem GetMenuItem(string txt,Image img)
{
ToolStripMenuItem menuItem = new ToolStripMenuItem();
menuItem.Text = txt;
menuItem.Image = img;
return menuItem;
}
/// 绑定右键菜单
/// </summary>
private void BindMenu()
{
ToolStripMenuItem menu0 = GetMenuItem("一级1", imageList1.Images[0]);
ToolStripMenuItem menu01 = GetMenuItem("二级11", imageList1.Images[1]);
menu01.Click += new EventHandler(toolStripMenuItem_Click);//添加事件
ToolStripMenuItem menu02 = GetMenuItem("二级12", imageList1.Images[2]);
menu0.DropDownItems.Add(menu01);
menu0.DropDownItems.Add(menu02);
cMenu.Items.Add(menu0);
ToolStripMenuItem menu1 = GetMenuItem("一级2", imageList1.Images[0]);
ToolStripMenuItem menu11 = GetMenuItem("二级21", imageList1.Images[1]);
ToolStripMenuItem menu12 = GetMenuItem("二级22", imageList1.Images[2]);
menu1.DropDownItems.Add(menu11);
menu1.DropDownItems.Add(menu12);
cMenu.Items.Add(menu1);
}
/// <summary>
/// 菜单项事件响应
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolStripMenuItem_Click(object sender, EventArgs e)
{
ToolStripMenuItem menu = sender as ToolStripMenuItem;
MessageBox.Show(menu.Text);
}
/// <summary>
/// 生成菜单项
/// </summary>
/// <param name="txt"></param>
/// <param name="img"></param>
/// <returns></returns>
private ToolStripMenuItem GetMenuItem(string txt,Image img)
{
ToolStripMenuItem menuItem = new ToolStripMenuItem();
menuItem.Text = txt;
menuItem.Image = img;
return menuItem;
}
我这个博客废弃不用了,今天想寻找外链的时候,突然想到这个博客权重很高。
有需要免费外链的,留言即可,我准备把这个博客变成免费的友情链接站点。