zoukankan      html  css  js  c++  java
  • 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、打开界面
    C++模版完全解析
    运维二三事儿
    tcpdump命令使用方法
    putty源码阅读----plink
    nginx--提供一键安装脚本
    vt100控制符
    zabbix---简介
    Dictionary
    装箱、拆箱
  • 原文地址:https://www.cnblogs.com/zeroone/p/3749967.html
Copyright © 2011-2022 走看看