首页 > 代码库 > create a C# context menu from code
create a C# context menu from code
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(); }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。