Saturday, October 4, 2014

Creating a Context Menu and Attaching it to a Control

10:16 AM - By azeheruddin khan 0

This example programmatically creates a Windows Forms context menu and associates it with a control(TextBox).

private void HomeForm_Load(object sender, EventArgs e)
            ContextMenu contextMenu = new ContextMenu();

            MenuItem menuItemCopy = new MenuItem();
            MenuItem menuItemCut = new MenuItem();
            MenuItem menuItemPaste = new MenuItem();
            MenuItem menuItemUndo = new MenuItem();

            contextMenu.MenuItems.AddRange(new MenuItem[] { menuItemCopy, menuItemCut, 
            menuItemPaste, menuItemUndo });

            menuItemCopy.Index = 0;
            menuItemCopy.Text = "Copy";
            menuItemCut.Index = 1;
            menuItemCut.Text = "Cut";
            menuItemPaste.Index = 2;
            menuItemPaste.Text = "Paste";
            menuItemUndo.Index = 3;
            menuItemUndo.Text = "Undo";

            txtName.ContextMenu = contextMenu;

About the Author

I am Azeheruddin Khan having more than 6 year experience in c#, and ms sql.My work comprise of medium and enterprise level projects using and other Microsoft .net technologies. Please feel free to contact me for any queries via posting comments on my blog,i will try to reply as early as possible. Follow me @fresher2programmer
View all posts by admin →

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post



© 2014 Fresher2Programmer. WP Theme-junkie converted by Bloggertheme9
Powered by Blogger.
back to top