How to add a contextMenu programmatically in C#(VS 2008)?

SandhraPrakash picture SandhraPrakash · Jun 13, 2013 · Viewed 19.9k times · Source

I'm using VS 2008, and I would like to add a contextMenu (definitely not ContextMenuStrip!!) to my application. There is no contextMenu (!!) in the toolbox. So how can I add it programmatically to my code?

Thanks In Advance,

Answer

Soner Gönül picture Soner Gönül · Jun 13, 2013

I assume you use Winforms, you can initialize ContextMenu class in your code. It has a 2 constructor which you can pass MenuItem as a parameter.

ContextMenu cm = new ContextMenu();

or

ContextMenu cm1 = new ContextMenu(new MenuItem[]);