Click here to Skip to main content
15,878,852 members
Articles / Programming Languages / XML

An XML-Driven Menu Strip

Rate me:
Please Sign up or sign in to vote.
4.71/5 (13 votes)
16 Apr 2007CPOL8 min read 127.7K   2.1K   75  
In this article, I'm going to show how to generate a dynamic MenuStrip (.NET 2.0's replacement of the MainMenu control) using recursion in C#. The MenuStrip will be based on XML data which contains all the details about the generated Menu.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MainMenuTestApp
{
    public partial class MenuStripEnhancedForm : Form
    {
        public MenuStripEnhancedForm()
        {
            InitializeComponent();
        }

        private void MenuStripEnhanced_Load( object sender, EventArgs e )
        {
            menuStripEnhanced1.LoadDynamicMenu();
        }

        private void MenuItemOnClick_LoadFileXMLAnonymous( object sender, EventArgs e )
        {
            menuStripEnhanced1.XmlPath = System.IO.Path.Combine( Application.StartupPath, "MenuAnonymous.xml" );
            menuStripEnhanced1.LoadDynamicMenu();
        }

        private void MenuItemOnClick_LoadFileXMLAdministrator( object sender, EventArgs e )
        {
            menuStripEnhanced1.XmlPath = System.IO.Path.Combine( Application.StartupPath, "MenuAdministrator.xml" );
            menuStripEnhanced1.LoadDynamicMenu();
        }

        private void MenuItemOnClick_LoadResourceXML( object sender, EventArgs e )
        {
            System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader( GetEmbeddedFile( System.Reflection.Assembly.GetExecutingAssembly().GetName().Name, "MenuEmbeddedResource.xml" ) );

            menuStripEnhanced1.LoadDynamicMenu( reader );
                
        }

        private System.IO.Stream GetEmbeddedFile( string assemblyName, string filename )
        {
            System.Reflection.Assembly assembly = System.Reflection.Assembly.Load( assemblyName );
            System.IO.Stream stream = assembly.GetManifestResourceStream( string.Format( "{0}.{1}", assemblyName, filename ) );

            if ( stream == null )
                throw new Exception( "Could not locate the embedded resource." );

            return stream;
        }

        private System.IO.Stream GetEmbeddedFile( System.Reflection.Assembly assembly, string filename )
        {
            string assemblyName = assembly.GetName().Name;
            return GetEmbeddedFile( assemblyName, filename );
        }

        public System.Xml.XmlTextReader GetEmbeddedXml( Type type, string filename )
        {
            System.IO.Stream stream = GetEmbeddedFile( type.Assembly.GetName().Name, filename );
            System.Xml.XmlTextReader xmlReader = new System.Xml.XmlTextReader( stream );

            return xmlReader;
        }

        private void MenuItemOnClick_Open( object sender, EventArgs e )
        {
            MessageBox.Show( "Open Clicked" );
        }

        private void MenuItemOnClick_Close( object sender, EventArgs e )
        {
            MessageBox.Show( "Close Clicked" );
        }

        private void MenuItemOnClick_Exit( object sender, EventArgs e )
        {
            Application.Exit();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
I've been working with C# since .NET Beta 1 and have been in the development scene since VB3.

I've always had a passion for development, technology, and computer as a whole. I consider myself one of the lucky few who actually love their job.

I've mostly been involed with web development in the corporate world, but I also work with Windows Forms development at home. Over the past 3 years or so the .NET applications I've developed have been backend maintenance applications. Transfering data, manipulating data, or doing some other back-end processes.

Comments and Discussions