码迷,mamicode.com
首页 > 其他好文 > 详细

create a C# context menu from code

时间:2014-05-26 00:36:55      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:class   c   code   tar   ext   a   

I try the one of your approach, it works well in my computer. Below is my code:

 public void AddContextMenu()
  {
   ContextMenu mnuContextMenu = new ContextMenu();
   mnuContextMenu.MenuItems.Add("&Red LED", new EventHandler(SetDisplayRed));
   mnuContextMenu.MenuItems.Add("&Blue LED", new EventHandler(SetDisplayBlue));
   mnuContextMenu.MenuItems.Add("&LCD", new EventHandler(SetDisplayLCD));
   textBox1.ContextMenu = mnuContextMenu;
   this.ContextMenu = mnuContextMenu;
  }

  private void SetDisplayRed(object sender, EventArgs e)
  {

   textBox1.BackColor = Color.Maroon;
   textBox1.ForeColor = Color.OrangeRed;
  }

  private void SetDisplayBlue(object sender, EventArgs e)
  {
   textBox1.BackColor = Color.Red;
   textBox1.ForeColor = Color.Gray;
  }

  private void SetDisplayLCD(object sender, EventArgs e)
  {
   textBox1.BackColor = Color.Beige;
   textBox1.ForeColor = Color.Blue;
  }

>When I right-click on the textbox I get a menu with Undo/Cut/Copy/Paste/Select All.  And when I right-click on the form I get nothing

Have you already added your "AddContextMenu()" method into the contruction method of your mainform?

public MainForm()
  {
   InitializeComponent();

   AddContextMenu();
  }

create a C# context menu from code,布布扣,bubuko.com

create a C# context menu from code

标签:class   c   code   tar   ext   a   

原文地址:http://www.cnblogs.com/zeroone/p/3749967.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!