cannot add items to ContextMenuStrip on inherited winform

873 views Asked by At

My setup is very simple.
I have a form called FormBaseList, on that form there is a DataGridView and a ContextMenuStrip. The ContextMenuStrip is coupled to the DataGridView and has 2 menuitems.

Now I add a new form to my project, using add Windows Form and then I choose "Windows Forms" / "Inherited Form". As base I choose my FormBaseList.

So now I have a new form, called FormSomethingList that is derived from FormBaseList.
In the visual designer I can now add an additional MenuItem to the ContextMenuStrip on FormSomethingList, but if I compile and run the application, that new MenuItem is gone. When I open FormSomeThingList in the designer the new MenuItem is also gone...

Is this "normal" behaviour or is there something wrong with my project ? I suspect the first but would like some confirmation. And if this is indeed "normal" behaviour, how can I workaround it without doing it all in code.

1

There are 1 answers

7
AudioBubble On BEST ANSWER

You don't even have to run the application. If you just rebuild, you will see that the menu option is gone. With each build, you are telling Form2 that it is a form 1, and the context menu gets set to what that is. In fact, notice that in form 2, the properties for the context menu are not editable. Unfortunately, the GUI allows you to type in additional values for the context menu, but if you try to change this by changing the "Items" collection property, in the property window, you will not be able to.

You will just need to add the item programatically. But that is not such a big deal. When you added the menu item in form 2, after you rebuilt, it disappeared from the context menu, but it is still there. Look in the designer code and you will see it. The menu item is still defined as part of form 2, but it's just been disconnected from the context menu. So on form2's load event, you can just readd it.

contextMenuStrip1.Items.Add(myAddedMenuStripItem);

Look in the designer.cs to see the name of the item you added to the context menu.