• WinForm XML


    开发环境:VS2008.NET2.0,网上找到的一个文章:Vs2005的win form动态生成菜单

    找了很久才找到这个两年前的文章,是通过定义Dataset实现,既然.NET有内置的XML对象,就另写了一个,

    另一个原因是,文中给出的XML文件,每条MenuItem都是由平行的MenuItemID和ParentItemID组成的,因为菜单是层次结构,而XML正好符合这个特性,

    本例只涉及两层菜单:

           private void CreateInstructionsListMenu(ToolStripMenuItem toolStripMenuItem_Instruments)
            {
                XmlDocument objXmlDoc = new XmlDocument(); 
                objXmlDoc.Load(@"..\..\Data\XMLInstructions.xml"); 
                XmlNodeList nodeList = objXmlDoc.SelectSingleNode("InstructionList").ChildNodes;
                foreach (XmlNode InstructionType in nodeList)
                {
                    ToolStripMenuItem TypeMenu = new ToolStripMenuItem();
                    TypeMenu.Text = InstructionType.Attributes["CNName"].Value;
                    foreach (XmlNode Instruction in InstructionType.ChildNodes)
                    {
                        ToolStripMenuItem InstructionMenu = new ToolStripMenuItem();
                        InstructionMenu.Text = Instruction.Attributes["Name"].Value;
                        InstructionMenu.ShortcutKeyDisplayString = Instruction.Attributes["GMName"].Value;
                        InstructionMenu.Tag = Instruction.Attributes["GMId"].Value;
                        InstructionMenu.Click += new EventHandler(InstructionMenu_Click);
                        TypeMenu.DropDownItems.Add(InstructionMenu);
                    }
                    toolStripMenuItem_Instruments.DropDownItems.Add(TypeMenu);
                }

            }

    相应的XML文件

    <?xml version= "1.0" encoding= "utf-8" ?>
    <InstructionList>
      <Type CNName="钢琴" GMName="&amp;Piano" AltKey="P">
        <Instruction GMId="0" AltKey="A" GMName="&amp;Acoustic Grand Piano" Name="大钢琴(声学钢琴)" />
        <Instruction GMId="1" AltKey="B" GMName="&amp;Bright Acoustic Piano" Name="明亮的钢琴"/>
        <Instruction GMId="2" AltKey="E" GMName="&amp;Electric Grand Piano" Name="电钢琴"/>
        <Instruction GMId="3" AltKey="O" GMName="H&amp;onky-tonk Piano" Name="酒吧钢琴"/>
        <Instruction GMId="4" AltKey="R" GMName="&amp;Rhodes Piano" Name="柔和的电钢琴"/>
        <Instruction GMId="5" AltKey="C" GMName="&amp;Chorused Piano" Name="加合唱效果的电钢琴"/>
        <Instruction GMId="6" AltKey="H" GMName="&amp;Harpsichord" Name="羽管键琴(拨弦古钢琴)"/>
        <Instruction GMId="7" AltKey="L" GMName="C&amp;lavichord" Name="科拉维科特琴(击弦古钢琴)"/>
      </Type>
      <Type CNName="色彩打击乐器" GMName="&amp;Chromatic Percussion" AltKey="C">
        <Instruction GMId="8" AltKey="C" GMName="&amp;Celesta" Name="钢片琴"/>
        <Instruction GMId="9" AltKey="G" GMName="&amp;Glockenspiel" Name="钟琴"/>
        <Instruction GMId="10" AltKey="B" GMName="Music &amp;box" Name="八音盒"/>
        <Instruction GMId="11" AltKey="V" GMName="&amp;Vibraphone" Name="颤音琴"/>
        <Instruction GMId="12" AltKey="M" GMName="&amp;Marimba" Name="马林巴"/>
        <Instruction GMId="13" AltKey="X" GMName="&amp;Xylophone" Name="木琴"/>
        <Instruction GMId="14" AltKey="T" GMName="&amp;Tubular Bells" Name="管钟"/>
        <Instruction GMId="15" AltKey="D" GMName="&amp;Dulcimer" Name="大扬琴"/>
      </Type>
      <Type CNName="风琴" GMName="&amp;Organ" AltKey="O">
        <Instruction GMId="16" AltKey="O" GMName="Hammond &amp;Organ" Name="击杆风琴"/>
        <Instruction GMId="17" AltKey="P" GMName="&amp;Percussive Organ" Name="打击式风琴"/>
        <Instruction GMId="18" AltKey="R" GMName="&amp;Rock Organ" Name="摇滚风琴"/>
        <Instruction GMId="19" AltKey="C" GMName="&amp;Church Organ" Name="教堂风琴"/>
        <Instruction GMId="20" AltKey="E" GMName="R&amp;eed Organ" Name="簧管风琴"/>
        <Instruction GMId="21" AltKey="A" GMName="&amp;Accordian" Name="手风琴"/>
        <Instruction GMId="22" AltKey="H" GMName="&amp;Harmonica" Name="口琴"/>
        <Instruction GMId="23" AltKey="T" GMName="&amp;Tango Accordian" Name="探戈手风琴"/>
      </Type>

    <InstructionList>

    效果截图:

  • 相关阅读:
    枚举、函数关于oracle函数listagg的使用说明by小雨
    执行、Mongodb MapReduce示例1个by小雨
    事务、异常TSQL 编码时应该注意的10个问题by小雨
    源、执行GoldenGate 单向DDL同步by小雨
    Oracle中的所有权限by小雨
    数据库、版本数据库学习从此开始by小雨
    统计、案例深入理解Oracle索引(10):索引列字符类型统计信息的32位限制by小雨
    字段、数据库表三大范式及存储方式by小雨
    数据库、用户第二章Getting Start with the Oracle Server(oracle入门)by小雨
    搜索、关键字截图留念,“万能数据库查询分析器”作为关键字在百度和谷歌上的海量搜索结果by小雨
  • 原文地址:https://www.cnblogs.com/kevinkim/p/2230001.html
Copyright © 2020-2023  润新知