Click here to Skip to main content
15,886,873 members
Articles / Programming Languages / C#

Code Template Add-In for Visual Studio .NET

Rate me:
Please Sign up or sign in to vote.
4.90/5 (71 votes)
6 May 200413 min read 465.1K   1.2K   150  
A Visual Studio .NET addin that provides a mechanism for inserting commonly used code snippets.
using System;
using System.Collections;
using System.Text;
using System.Windows.Forms;

namespace CodeTemplate
{
	internal class TemplatePopupMenu: ContextMenu
	{		
        public TemplatePopupMenu()
		{
            m_scopes = new Stack();
            m_scopes.Push(MenuItems);
        }

        public void Clear()
        {
            if(MenuItems.Count > 0)
                MenuItems.Clear();
        }

		public void AddSeparator()
		{
			Add("-");
		}

		public void Add(String name)
		{
			Add(name, null, 0);
		}

        public void Add(String name, String text)
        {
            Add(name, text, 0);
        }

        public void Add(String name, String text, Int32 id)
        {
            CurrentScope.Add(new TemplateMenuItem(name, text, m_eh, id));
        }

		public void Update(String text)
		{
			m_mi.TemplateText = text;
			m_mi.Click += m_eh;
		}

        public TemplateMenuItem EnterScope(String name)
        {
            m_mi = new TemplateMenuItem(name);
            CurrentScope.Add(m_mi);

            m_scopes.Push(m_mi.MenuItems);
			return m_mi;
        }

        public void LeaveScope()
        {
            m_scopes.Pop();
        }

        public Boolean EnableTemplateItems
        {
            set
            {
                foreach(TemplateMenuItem mi in MenuItems)
                {
                    if(mi.Id >= 0)
                        mi.Enabled = value;
                }
            }
        }

        public Boolean IsBalanced 
        { 
            get { return CurrentScope == MenuItems; }
        }

        public Menu.MenuItemCollection CurrentScope
        {
            get { return (Menu.MenuItemCollection)m_scopes.Peek(); }
        }

		public void PopAll()
		{
			m_scopes.Clear();
			m_scopes.Push(MenuItems);
		}

        public EventHandler EventHandler
        {
            get { return m_eh; }
            set { m_eh = value; }
        }

        private Stack m_scopes;
        private EventHandler m_eh;
		private TemplateMenuItem m_mi;
	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions