带历史信息的菜单

实现效果:

  

知识运用:

  ToolStripMenuItem菜单项中DropDownItems集合的Insert方法

  public void Insert( int index,ToolStripItem value )

实现代码:

        private void Add_Items() {
            if (File.Exists(address)) {
                StreamReader sr = new StreamReader(address);                        //创建流读取器对象
                int i = 文件FToolStripMenuItem.DropDownItems.Count - 2;             //得到菜单项索引
                while(sr.Peek()>=0){
                    ToolStripMenuItem ts = new ToolStripMenuItem(sr.ReadLine());    //创建菜单项集合
                    ts.BackColor = Color.GreenYellow;                               //设置其背景
                    文件FToolStripMenuItem.DropDownItems.Insert(i,ts);              //向菜单中添加新项
                    ts.Click += new EventHandler(MenuItem_Click);                   //添加单击事件
                }
                sr.Close();                                                         //关闭流
            }
        }
原文地址:https://www.cnblogs.com/feiyucha/p/10175617.html