contextmenustrip can you share them across forms?

I don't want to add them to a form.  I want to have some common place to create them and have various forms use them.  Is this possible?
jackjohnson44Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
Sure. It's just a class, so you could create an instance in some common class, and then just make them accessible (via an instance or perhaps as static) to each form. Then just assign them to the ContextMenu property of the object that you want to have them.
0
jackjohnson44Author Commented:
Yes, but aren't they built in the designer?  Would I have to create them on a separate form?
0
käµfm³d 👽Commented:
They can be, but since they themselves are classes you can build them in code as well.

e.g.

New Class
using System.Windows.Forms;

namespace _28233245
{
    public static class ContextMenus
    {
        private static ContextMenu firstContextMenu;
        private static ContextMenu secondContextMenu;

        public static ContextMenu FirstContextMenu
        {
            get
            {
                if (firstContextMenu == null)   // lazy initialization
                {
                    MenuItem[] items =
                    {
                        new MenuItem("Menu Option 1"),
                        new MenuItem("Menu Option 2"),
                        new MenuItem("Menu Option 3"),
                    };

                    firstContextMenu = new ContextMenu(items);
                }

                return firstContextMenu;
            }
        }

        public static ContextMenu SecondContextMenu
        {
            get
            {
                if (secondContextMenu == null)   // lazy initialization
                {
                    MenuItem[] items =
                    {
                        new MenuItem("Another Menu Option 1"),
                        new MenuItem("Another Menu Option 2"),
                        new MenuItem("Another Menu Option 3"),
                    };

                    secondContextMenu = new ContextMenu(items);
                }

                return secondContextMenu;
            }
        }
    }
}

Open in new window


Form
using System;
using System.Windows.Forms;

namespace _28233245
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnFirstCtx_Click(object sender, EventArgs e)
        {
            this.textBox1.ContextMenu = ContextMenus.FirstContextMenu;
        }

        private void btnSecondCtx_Click(object sender, EventArgs e)
        {
            this.textBox1.ContextMenu = ContextMenus.SecondContextMenu;
        }
    }
}

Open in new window


Demo
The second image is before I have assigned any context menu to the text box. The subsequent two images are assigning the first context menu and then the second context menu, respectively.

As you might discern from my screenshot, you have to now set the context menu for a control by way of code, not the designer. But the designer is just a convenience for you (the developer) anyway--it sets the context menu in code on your behalf when you modify the context menu via the designer.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
käµfm³d 👽Commented:
P.S.

My use of a static class and properties above was only as an example. You could take that approach if you wish, but you don't have to. You could easily make those instance properties and a non-static class if you desired to.
0
jackjohnson44Author Commented:
Thanks.  I have something similar to that already.  I was actually asking about a contextmenustrip and not a context menu.  I saw some posts someplace and it said it was the replacement for the contextmenu.  It looks like it has more advanced features.  I was looking to use that.

I realize you can still use contextmenu but I like to incorporate the latest features for learning purposes.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.