Subclass MenuItem

   
 

using System;
using System.Drawing;
using System.Windows.Forms;
   
class BetterContextMenu: Form
{
     MenuItemColor micColor;
   
     public static void Main()
     {
          Application.Run(new BetterContextMenu());
     }
     public BetterContextMenu()
     {
          Text = "Better Context Menu Demo";
   
          EventHandler eh = new EventHandler(MenuColorOnClick);
   
          MenuItemColor[] amic = 
          {
               new MenuItemColor(Color.Black,   "&Black",   eh),
               new MenuItemColor(Color.Blue,    "B&lue",    eh),
               new MenuItemColor(Color.Green,   "&Green",   eh),
               new MenuItemColor(Color.Cyan,    "&Cyan",    eh),
               new MenuItemColor(Color.Red,     "&Red",     eh),
               new MenuItemColor(Color.Magenta, "&Magenta", eh),
               new MenuItemColor(Color.Yellow,  "&Yellow",  eh),
               new MenuItemColor(Color.White,   "&White",   eh)
          };
   
          foreach (MenuItemColor mic in amic)
               mic.RadioCheck = true;
   
          micColor = amic[3];
          micColor.Checked = true;
          BackColor = micColor.Color;
   
          ContextMenu = new ContextMenu(amic);
     }
     void MenuColorOnClick(object obj, EventArgs ea)
     {
          micColor.Checked = false;
          micColor = (MenuItemColor) obj;
          micColor.Checked = true;
   
          BackColor = micColor.Color;
     }
}
class MenuItemColor: MenuItem
{
     Color clr;
   
     public MenuItemColor(Color clr, string str, EventHandler eh):
                                                            base(str, eh)
     {
          Color = clr;
     }
     public Color Color
     {
          get { return clr; }
          set { clr = value; }
     }
}

    


Font Menu

using System;
using System.Drawing;
using System.Windows.Forms;

class FontMenu: Form
{
const int iPointSize = 24;
string strFacename;

public static void Main()
{
Application.Run(new FontMenu());
}
public FontMenu()
{
strFacename = Font.Name;

Menu = new MainMenu();

MenuItem mi = new MenuItem(“&Facename”);
mi.Popup += new EventHandler(MenuFacenameOnPopup);
mi.MenuItems.Add(” “); // Necessary for pop-up call
Menu.MenuItems.Add(mi);
}
void MenuFacenameOnPopup(object obj, EventArgs ea)
{
MenuItem miFacename = (MenuItem)obj;
FontFamily[] aff = FontFamily.Families;
EventHandler ehClick = new EventHandler(MenuFacenameOnClick);
MenuItem[] ami = new MenuItem[aff.Length];

for (int i = 0; i < aff.Length; i++) { ami[i] = new MenuItem(aff[i].Name); ami[i].Click += ehClick; if (aff[i].Name == strFacename) ami[i].Checked = true; } miFacename.MenuItems.Clear(); miFacename.MenuItems.AddRange(ami); } void MenuFacenameOnClick(object obj, EventArgs ea) { MenuItem mi = (MenuItem)obj; strFacename = mi.Text; Invalidate(); } protected override void OnPaint(PaintEventArgs pea) { Graphics grfx = pea.Graphics; Font font = new Font(strFacename, iPointSize); StringFormat strfmt = new StringFormat(); grfx.DrawString("Sample Text", font, new SolidBrush(ForeColor), ClientRectangle, strfmt); } } [/csharp]

Set MenuItem ShortCut

   
 

using System;
using System.Drawing;
using System.Windows.Forms;
   
class StandardMenu: Form
{
     MenuItem miFileOpen, miFileSave;
     MenuItem miEditCut, miEditCopy, miEditPaste;
   
     bool bDocumentPresent  = true;
     bool bNonNullSelection = true;
     bool bStuffInClipboard = false;
   
     public static void Main()
     {
          Application.Run(new StandardMenu());
     }
     public StandardMenu()
     {
          Menu = new MainMenu();
   
          MenuItem mi = new MenuItem("&amp;File");
          mi.Popup += new EventHandler(MenuFileOnPopup);
          Menu.MenuItems.Add(mi);
          int index = Menu.MenuItems.Count - 1;
   
          miFileOpen = new MenuItem("&amp;Open...");
          miFileOpen.Click += new EventHandler(MenuFileOpenOnClick);
          miFileOpen.Shortcut = Shortcut.CtrlO;
          Menu.MenuItems[index].MenuItems.Add(miFileOpen);
   
          miFileSave  = new MenuItem("&amp;Save");
          miFileSave.Click += new EventHandler(MenuFileSaveOnClick);
          miFileSave.Shortcut = Shortcut.CtrlS;
          Menu.MenuItems[index].MenuItems.Add(miFileSave);
   
          mi = new MenuItem("-");
          Menu.MenuItems[index].MenuItems.Add(mi);
   
          mi = new MenuItem("E&amp;xit");
          mi.Click += new EventHandler(MenuFileExitOnClick);
          Menu.MenuItems[index].MenuItems.Add(mi);
   
          mi = new MenuItem("&amp;Edit");
          mi.Popup += new EventHandler(MenuEditOnPopup);
          Menu.MenuItems.Add(mi);
          index = Menu.MenuItems.Count - 1;
   
          miEditCut = new MenuItem("Cu&amp;t");
          miEditCut.Click += new EventHandler(MenuEditCutOnClick);
          miEditCut.Shortcut = Shortcut.CtrlX;
          Menu.MenuItems[index].MenuItems.Add(miEditCut);
   
          miEditCopy = new MenuItem("&amp;Copy");
          miEditCopy.Click += new EventHandler(MenuEditCopyOnClick);
          miEditCopy.Shortcut = Shortcut.CtrlC;
          Menu.MenuItems[index].MenuItems.Add(miEditCopy);
   
          miEditPaste = new MenuItem("&amp;Paste");
          miEditPaste.Click += new EventHandler(MenuEditCopyOnClick);
          miEditPaste.Shortcut = Shortcut.CtrlV;
          Menu.MenuItems[index].MenuItems.Add(miEditPaste);
   
          mi = new MenuItem("&amp;Help");
          Menu.MenuItems.Add(mi);
          index = Menu.MenuItems.Count - 1;
   
          mi = new MenuItem("&amp;About StandardMenu...");
          mi.Click += new EventHandler(MenuHelpAboutOnClick);
          Menu.MenuItems[index].MenuItems.Add(mi);
     }
     void MenuFileOnPopup(object obj, EventArgs ea)
     {
          miFileSave.Enabled = bDocumentPresent;
     }
     void MenuEditOnPopup(object obj, EventArgs ea)
     {
          miEditCut.Enabled = bNonNullSelection;
          miEditCopy.Enabled = bNonNullSelection;
          miEditPaste.Enabled = bStuffInClipboard;
     }
     void MenuFileOpenOnClick(object obj, EventArgs ea)
     { 
          MessageBox.Show("File Open dialog box!", Text);
     }
     void MenuFileSaveOnClick(object obj, EventArgs ea)
     {
          MessageBox.Show("File Save dialog box!", Text);
     }
     void MenuFileExitOnClick(object obj, EventArgs ea)
     {
          Close();
     }
     void MenuEditCutOnClick(object obj, EventArgs ea)
     {
     }
     void MenuEditCopyOnClick(object obj, EventArgs ea)
     {
     }
     void MenuEditPasteOnClick(object obj, EventArgs ea)
     {
     }
     void MenuHelpAboutOnClick(object obj, EventArgs ea)
     {
          MessageBox.Show("StandardMenu ?", Text);
     }
}         

    


Creating a menu on the fly.

   
 
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class Form1 : System.Windows.Forms.Form {
    private System.Windows.Forms.MainMenu File = new System.Windows.Forms.MainMenu();
    private System.Windows.Forms.MenuItem menuItem1 = new System.Windows.Forms.MenuItem();
    private System.Windows.Forms.MenuItem menuItem2 = new System.Windows.Forms.MenuItem();
    private System.Windows.Forms.MenuItem menuItem3 = new System.Windows.Forms.MenuItem();
    private System.Windows.Forms.Button button1 = new System.Windows.Forms.Button();
    private System.Windows.Forms.Button button2 = new System.Windows.Forms.Button();
    private System.ComponentModel.Container components = null;
    private int nIndex = 0;

    public Form1() {
        this.SuspendLayout();
        this.File.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {this.menuItem1});
        this.menuItem1.Index = 0;
        this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem2, this.menuItem3});
        this.menuItem1.Text = "File";
        this.menuItem2.Index = 0;
        this.menuItem2.Text = "&amp;Close";
        this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
        this.menuItem3.Index = 1;
        this.menuItem3.Text = "E&amp;xit";
        this.button1.Location = new System.Drawing.Point(40, 208);
        this.button1.Name = "button1";
        this.button1.TabIndex = 0;
        this.button1.Text = "&amp;Add";
        this.button1.Click += new System.EventHandler(this.button1_Click);
        this.button2.Location = new System.Drawing.Point(176, 208);
        this.button2.Name = "button2";
        this.button2.TabIndex = 1;
        this.button2.Text = "&amp;Close";
        this.button2.Click += new System.EventHandler(this.button2_Click);
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(292, 273);
        this.Controls.AddRange(new System.Windows.Forms.Control[] { this.button2, this.button1});
        this.Menu = this.File;
        this.ResumeLayout(false);
    }

    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }

    private void button2_Click(object sender, System.EventArgs e) {
        Close();
    }

    private void menuItemHandler(object sender, System.EventArgs e) {
        MessageBox.Show(this, "Menu Handler Called");
        MenuItem mi = (MenuItem)sender;
        MessageBox.Show(this, "Menu Item: " + mi.Text);
    }

    private void button1_Click(object sender, System.EventArgs e) {
        MenuItem mi = new MenuItem("File " + (nIndex + 1), new EventHandler(menuItemHandler));
        this.menuItem1.MenuItems.Add(mi);
        nIndex++;
    }

    private void menuItem2_Click(object sender, System.EventArgs e) {
        Close();
    }
}

    


Dynamic Menu

   

/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald

Publisher: Apress
ISBN: 1590590457
*/

using System.Data.SqlClient;

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace DynamicMenu
{
    /// <summary>
    /// Summary description for DynamicMenu.
    /// </summary>
    public class DynamicMenu : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.MainMenu mnuMain;
        internal System.Windows.Forms.MenuItem mnuFile;
        internal System.Windows.Forms.MenuItem mnuNew;
        internal System.Windows.Forms.MenuItem mnuOpen;
        internal System.Windows.Forms.MenuItem mnuClose;
        internal System.Windows.Forms.MenuItem mnuSave;
        internal System.Windows.Forms.MenuItem MenuItem12;
        internal System.Windows.Forms.MenuItem mnuExit;
        internal System.Windows.Forms.MenuItem mnuTools;
        internal System.Windows.Forms.MenuItem mnuManageHardware;
        internal System.Windows.Forms.MenuItem mnuSetupUserAccounts;
        internal System.Windows.Forms.MenuItem mnuChangeDisplay;
        internal System.Windows.Forms.MenuItem mnuHelp;
        internal System.Windows.Forms.MenuItem mnuContents;
        internal System.Windows.Forms.MenuItem MenuItem13;
        internal System.Windows.Forms.MenuItem mnuAbout;
        internal System.Windows.Forms.Button cmdAdmin;
        internal System.Windows.Forms.Button cmdUser;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public DynamicMenu()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.mnuMain = new System.Windows.Forms.MainMenu();
            this.mnuFile = new System.Windows.Forms.MenuItem();
            this.mnuNew = new System.Windows.Forms.MenuItem();
            this.mnuOpen = new System.Windows.Forms.MenuItem();
            this.mnuClose = new System.Windows.Forms.MenuItem();
            this.mnuSave = new System.Windows.Forms.MenuItem();
            this.MenuItem12 = new System.Windows.Forms.MenuItem();
            this.mnuExit = new System.Windows.Forms.MenuItem();
            this.mnuTools = new System.Windows.Forms.MenuItem();
            this.mnuManageHardware = new System.Windows.Forms.MenuItem();
            this.mnuSetupUserAccounts = new System.Windows.Forms.MenuItem();
            this.mnuChangeDisplay = new System.Windows.Forms.MenuItem();
            this.mnuHelp = new System.Windows.Forms.MenuItem();
            this.mnuContents = new System.Windows.Forms.MenuItem();
            this.MenuItem13 = new System.Windows.Forms.MenuItem();
            this.mnuAbout = new System.Windows.Forms.MenuItem();
            this.cmdAdmin = new System.Windows.Forms.Button();
            this.cmdUser = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // mnuMain
            // 
            this.mnuMain.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                                                                    this.mnuFile,
                                                                                    this.mnuTools,
                                                                                    this.mnuHelp});
            // 
            // mnuFile
            // 
            this.mnuFile.Index = 0;
            this.mnuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                                                                    this.mnuNew,
                                                                                    this.mnuOpen,
                                                                                    this.mnuClose,
                                                                                    this.mnuSave,
                                                                                    this.MenuItem12,
                                                                                    this.mnuExit});
            this.mnuFile.Text = "File";
            // 
            // mnuNew
            // 
            this.mnuNew.Index = 0;
            this.mnuNew.Text = "New";
            // 
            // mnuOpen
            // 
            this.mnuOpen.Index = 1;
            this.mnuOpen.Text = "Open";
            // 
            // mnuClose
            // 
            this.mnuClose.Index = 2;
            this.mnuClose.Text = "Close";
            // 
            // mnuSave
            // 
            this.mnuSave.Index = 3;
            this.mnuSave.Text = "Save";
            // 
            // MenuItem12
            // 
            this.MenuItem12.Index = 4;
            this.MenuItem12.Text = "-";
            // 
            // mnuExit
            // 
            this.mnuExit.Index = 5;
            this.mnuExit.Text = "Exit";
            // 
            // mnuTools
            // 
            this.mnuTools.Index = 1;
            this.mnuTools.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                                                                     this.mnuManageHardware,
                                                                                     this.mnuSetupUserAccounts,
                                                                                     this.mnuChangeDisplay});
            this.mnuTools.Text = "Tools";
            // 
            // mnuManageHardware
            // 
            this.mnuManageHardware.Index = 0;
            this.mnuManageHardware.Text = "Manage Hardware";
            // 
            // mnuSetupUserAccounts
            // 
            this.mnuSetupUserAccounts.Index = 1;
            this.mnuSetupUserAccounts.Text = "Setup User Accounts";
            // 
            // mnuChangeDisplay
            // 
            this.mnuChangeDisplay.Index = 2;
            this.mnuChangeDisplay.Text = "Change Display";
            // 
            // mnuHelp
            // 
            this.mnuHelp.Index = 2;
            this.mnuHelp.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                                                                    this.mnuContents,
                                                                                    this.MenuItem13,
                                                                                    this.mnuAbout});
            this.mnuHelp.Text = "Help";
            // 
            // mnuContents
            // 
            this.mnuContents.Index = 0;
            this.mnuContents.Text = "Contents";
            // 
            // MenuItem13
            // 
            this.MenuItem13.Index = 1;
            this.MenuItem13.Text = "-";
            // 
            // mnuAbout
            // 
            this.mnuAbout.Index = 2;
            this.mnuAbout.Text = "About";
            // 
            // cmdAdmin
            // 
            this.cmdAdmin.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.cmdAdmin.Location = new System.Drawing.Point(132, 88);
            this.cmdAdmin.Name = "cmdAdmin";
            this.cmdAdmin.Size = new System.Drawing.Size(80, 24);
            this.cmdAdmin.TabIndex = 3;
            this.cmdAdmin.Text = "Admin Level";
            this.cmdAdmin.Click += new System.EventHandler(this.cmdAdmin_Click);
            // 
            // cmdUser
            // 
            this.cmdUser.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.cmdUser.Location = new System.Drawing.Point(44, 88);
            this.cmdUser.Name = "cmdUser";
            this.cmdUser.Size = new System.Drawing.Size(80, 24);
            this.cmdUser.TabIndex = 2;
            this.cmdUser.Text = "User Level";
            this.cmdUser.Click += new System.EventHandler(this.cmdUser_Click);
            // 
            // DynamicMenu
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
            this.ClientSize = new System.Drawing.Size(256, 129);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.cmdAdmin,
                                                                          this.cmdUser});
            this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Menu = this.mnuMain;
            this.Name = "DynamicMenu";
            this.Text = "Dynamic Menu";
            this.Load += new System.EventHandler(this.DynamicMenu_Load);
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new DynamicMenu());
        }

        private void DynamicMenu_Load(object sender, System.EventArgs e)
        {
            cmdUser_Click(null,null);

        }

        private void cmdUser_Click(object sender, System.EventArgs e)
        {
            DataTable dtPermissions;

            // Get permissions for an Admin-level user.
            dtPermissions = DBPermissions.GetPermissions(DBPermissions.Level.User);

            // Update the menu with these permissions.
            SearchMenu(this.Menu, dtPermissions);

        }

        private void cmdAdmin_Click(object sender, System.EventArgs e)
        {
            DataTable dtPermissions;

            // Get permissions for an Admin-level user.
            dtPermissions = DBPermissions.GetPermissions(DBPermissions.Level.Admin);

            // Update the menu with these permissions.
            SearchMenu(this.Menu, dtPermissions);

        }

        private void SearchMenu(Menu menu, DataTable dtPermissions)
        {
            DataRow[] rowMatch;

            foreach (MenuItem mnuItem in menu.MenuItems)
            {           
                // See if this menu item has a corresponding row.
                rowMatch = dtPermissions.Select("ControlName = &#039;" + mnuItem.Text + "&#039;");

                // If it does, configure the menu item state accordingly.           
                if (rowMatch.GetLength(0) > 0)
                {
                    DBPermissions.State state;
                    state = (DBPermissions.State)int.Parse(rowMatch[0]["State"].ToString());

                    switch (state)
                    {
                        case DBPermissions.State.Hidden:
                            mnuItem.Visible = false;
                            break;
                        case DBPermissions.State.Disabled:
                            mnuItem.Enabled = false;
                            break;
                    }
                }
                else
                {
                    mnuItem.Visible = true;
                    mnuItem.Enabled = true;
                }

                // Search recursively through any submenus.
                if (mnuItem.MenuItems.Count > 0)
                {
                    SearchMenu(mnuItem, dtPermissions);
                }
            }
        }

    }
    public class DBPermissions
    {
        public enum State
        {
            Normal = 0,
            Disabled = 1,
            Hidden = 2
        }

        public enum Level
        {
            Admin,
            User
        }

        private static SqlConnection con = new SqlConnection("Data Source=localhost;"
            + "Integrated Security=SSPI;Initial Catalog=Apress;");

        public static DataTable GetPermissions(Level userLevel)
        {
            con.Open();

            // Permissions isn&#039;t actually actually a table in our data source.
            // Instead, it&#039;s a view that combines the important information
            // from all three tables using a Join query.
            string selectPermissions = "SELECT * FROM Permissions ";

            switch (userLevel)
            {
                case Level.Admin:
                    selectPermissions += "WHERE LevelName = &#039;Admin&#039;";
                    break;
                case Level.User:
                    selectPermissions += "WHERE LevelName = &#039;User&#039;";
                    break;
            }

            SqlCommand cmd = new SqlCommand(selectPermissions, con);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            adapter.Fill(ds, "Permissions");

            con.Close();

            return ds.Tables["Permissions"];
        }
    }

}
/*
-- Database Dump Wizard Ver 2.2.1 (libmssql version 2.3)
--
-- Host: localhost    Database: [Apress]
-- ---------------------------------------------------------
-- Server version   Microsoft SQL Server Version 8.0.255


-- Dumping user-defined datatypes in &#039;[Apress]&#039;


-- Table structure for table &#039;Controls&#039;
CREATE TABLE [Controls] (
[ID] int IDENTITY NOT NULL,
[ControlName] varchar(20) NOT NULL)
GO

CREATE UNIQUE CLUSTERED INDEX PK_Controls ON [Controls] (ID)
GO


-- Dumping data for table &#039;Controls&#039;
--

-- Enable identity insert
SET IDENTITY_INSERT [Controls] ON
GO

INSERT INTO [Controls] ([ID], [ControlName])
VALUES(1, &#039;New&#039;)
GO
INSERT INTO [Controls] ([ID], [ControlName])
VALUES(2, &#039;Save&#039;)
GO
INSERT INTO [Controls] ([ID], [ControlName])
VALUES(3, &#039;Manage Hardware&#039;)
GO
INSERT INTO [Controls] ([ID], [ControlName])
VALUES(4, &#039;Setup User Accounts&#039;)
GO

-- Disable identity insert
SET IDENTITY_INSERT [Controls] OFF
GO



-- Table structure for table &#039;Controls_Levels&#039;
CREATE TABLE [Controls_Levels] (
[ID] int IDENTITY NOT NULL,
[Control_ID] int NOT NULL,
[Level_ID] int NOT NULL,
[State] smallint)
GO

CREATE UNIQUE CLUSTERED INDEX PK_Control_Levels ON [Controls_Levels] (ID)
GO


-- Dumping data for table &#039;Controls_Levels&#039;
--

-- Enable identity insert
SET IDENTITY_INSERT [Controls_Levels] ON
GO

INSERT INTO [Controls_Levels] ([ID], [Control_ID], [Level_ID], [State])
VALUES(1, 1, 2, 1)
GO
INSERT INTO [Controls_Levels] ([ID], [Control_ID], [Level_ID], [State])
VALUES(2, 2, 2, 1)
GO
INSERT INTO [Controls_Levels] ([ID], [Control_ID], [Level_ID], [State])
VALUES(3, 3, 2, 2)
GO
INSERT INTO [Controls_Levels] ([ID], [Control_ID], [Level_ID], [State])
VALUES(4, 4, 2, 2)
GO

-- Disable identity insert
SET IDENTITY_INSERT [Controls_Levels] OFF
GO



-- Table structure for table &#039;Levels&#039;
CREATE TABLE [Levels] (
[ID] int IDENTITY NOT NULL,
[LevelName] varchar(50))
GO

CREATE UNIQUE CLUSTERED INDEX PK_UserLevels ON [Levels] (ID)
GO


-- Dumping data for table &#039;Levels&#039;
--

-- Enable identity insert
SET IDENTITY_INSERT [Levels] ON
GO

INSERT INTO [Levels] ([ID], [LevelName])
VALUES(1, &#039;Admin&#039;)
GO
INSERT INTO [Levels] ([ID], [LevelName])
VALUES(2, &#039;User&#039;)
GO

-- Disable identity insert
SET IDENTITY_INSERT [Levels] OFF
GO


CREATE VIEW [Permissions]
AS
SELECT     Controls.ControlName, Levels.LevelName, Controls_Levels.State
FROM         Controls INNER JOIN
                      Controls_Levels ON Controls.ID = Controls_Levels.Control_ID INNER JOIN
                      Levels ON Controls_Levels.Level_ID = Levels.ID
GO


*/

           
          


Menu Text Provider Host


   

/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald

Publisher: Apress
ISBN: 1590590457
*/

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace ExtenderProviderHost
{
    /// <summary>
    /// Summary description for MenuTextProviderHost.
    /// </summary>
    public class MenuTextProviderHost : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.MainMenu mnuMain;
        internal System.Windows.Forms.MenuItem MenuItem1;
        internal System.Windows.Forms.MenuItem mnuNew;
        internal System.Windows.Forms.MenuItem mnuOpen;
        internal System.Windows.Forms.MenuItem mnuSave;
        private MenuTextProvider menuTextProvider1;
        internal System.Windows.Forms.GroupBox GroupBox1;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public MenuTextProviderHost()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.mnuMain = new System.Windows.Forms.MainMenu();
            this.MenuItem1 = new System.Windows.Forms.MenuItem();
            this.mnuNew = new System.Windows.Forms.MenuItem();
            this.mnuOpen = new System.Windows.Forms.MenuItem();
            this.mnuSave = new System.Windows.Forms.MenuItem();
            this.menuTextProvider1 = new MenuTextProvider();
            this.GroupBox1 = new System.Windows.Forms.GroupBox();
            this.SuspendLayout();
            // 
            // mnuMain
            // 
            this.mnuMain.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                                                                    this.MenuItem1});
            // 
            // MenuItem1
            // 
            this.MenuItem1.Index = 0;
            this.MenuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                                                                      this.mnuNew,
                                                                                      this.mnuOpen,
                                                                                      this.mnuSave});
            this.MenuItem1.Text = "File";
            // 
            // mnuNew
            // 
            this.mnuNew.Index = 0;
            this.mnuNew.Text = "New";
            // 
            // mnuOpen
            // 
            this.mnuOpen.Index = 1;
            this.mnuOpen.Text = "Open";
            // 
            // mnuSave
            // 
            this.mnuSave.Index = 2;
            this.mnuSave.Text = "Save";
            // 
            // menuTextProvider1
            // 
            this.menuTextProvider1.Location = new System.Drawing.Point(0, 244);
            this.menuTextProvider1.Name = "menuTextProvider1";
            this.menuTextProvider1.Size = new System.Drawing.Size(292, 22);
            this.menuTextProvider1.TabIndex = 0;
            this.menuTextProvider1.Text = "menuTextProvider1";
            // 
            // GroupBox1
            // 
            this.GroupBox1.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
                | System.Windows.Forms.AnchorStyles.Right);
            this.GroupBox1.Location = new System.Drawing.Point(0, 240);
            this.GroupBox1.Name = "GroupBox1";
            this.GroupBox1.Size = new System.Drawing.Size(292, 4);
            this.GroupBox1.TabIndex = 2;
            this.GroupBox1.TabStop = false;
            this.GroupBox1.Text = "GroupBox1";
            // 
            // MenuTextProviderHost
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.GroupBox1,
                                                                          this.menuTextProvider1});
            this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Menu = this.mnuMain;
            this.Name = "MenuTextProviderHost";
            this.Text = "MenuTextProviderHost";
            this.Load += new System.EventHandler(this.MenuTextProviderHost_Load);
            this.ResumeLayout(false);

        }
        #endregion

        private void MenuTextProviderHost_Load(object sender, System.EventArgs e)
        {
            menuTextProvider1.SetHelpText(mnuNew, " Create a new document and abandon the current one.");
        }

        static void Main() 
        {
            Application.Run(new MenuTextProviderHost());
        }
    }
    [ProvideProperty("HelpText", typeof(string))]
    public class MenuTextProvider : StatusBar, IExtenderProvider
    {
        public bool CanExtend(object extendee)
        {
            if (extendee.GetType() == typeof(MenuItem))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private Hashtable helpText = new Hashtable();

        public void SetHelpText(object extendee, string value)
        {
            // Specifying an empty value removes the extension.
            if (value == "")
            {
                helpText.Remove(extendee);
                MenuItem mnu = (MenuItem)extendee;
                mnu.Select -= new EventHandler(MenuSelect);
            }
            else
            {
                helpText[extendee] = value;
                MenuItem mnu = (MenuItem)extendee;
                mnu.Select += new EventHandler(MenuSelect);
            }
        }

        public string GetHelpText(object extendee)
        {
            if (helpText[extendee] != null)
            {
                return helpText[extendee].ToString();
            }
            else
            {
                return string.Empty;
            }
            }

            private void MenuSelect(object sender, System.EventArgs e)
            {
                this.Text = helpText[sender].ToString();
            }

    }




}