One contextMenuStrip for different C# forms
Variant of placing one contextMenuStrip on different forms for different elements.
In the main namespace, we add a class with contextMenuStrip:
namespace TestContextMenu
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
static class Menushki
{
public static ContextMenuStrip menu1 = new ContextMenuStrip();
}
}
In our case, this is the Menushki class
Then, when the application loads, we create the menu itself, you can programmatically set all elements and parameters, but in my opinion, it's easier to do in the constructor and when loading the first form, simply assign:
private void Form1_Load(object sender, EventArgs e)
{
Menushki.menu1 = contextMenuStrip1;
}
Next, if you want to use this menu in any form, simply associate it with the necessary element when loading the form. You can also change it from any form:
private void Form2_Load(object sender, EventArgs e)
{
this.ContextMenuStrip = Menushki.menu1;
}
Here's how it looks in the example:
Download example:
testcontextmenu.rar · 17 downloads
- testcontextmenu.rar 36.8 KB 17 downloads
Comments (0)
No comments yet.