0

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.

GuidoG
  • 11,359
  • 6
  • 44
  • 79
  • If you downvote than also tell me why, else there is nothing to learn from it. Also, why downvote an old post with an accepted answer ? – GuidoG Nov 06 '15 at 16:59

1 Answers1

1

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.

  • I was already afraid for such an answer. Why do they call it a visual designer ? Anyway, my workaround now is to put a dummy ContextMenuStrip on the inherited form and than in the load event I merge this with the inherited ContextMenuStrip using ToolStripManager.Merge() – GuidoG Aug 18 '15 at 14:57
  • Using merge is a good idea. As for the visual designer, mind you, they are allowing you to visually edit the properties of that context menu. They are just limiting it to the form where you originally added that context menu. In inherited forms, VS does not let you mess with the properties of some widgets. There are good reasons why this cannot be done reliably. –  Aug 18 '15 at 15:01
  • Hey, here is a change on this. If you add that menu item in the gui, and specify its event handler by double clicking on it, it gets taken out of the context menu when you rebuild, but it is still created in Form2 as an additional property of Form2, so you could just programmatically readd it. I am editing my answer to reflect this. –  Aug 18 '15 at 15:07
  • I will still have to write a line of code, and if more than one menuitem is added I will have to write more than one line of code. I like the merge better because I can still use the visual designer and only need one line of code no matter how many items I have added. – GuidoG Aug 18 '15 at 15:16
  • Maybe it is not such a big deal, but if you are coming from Delphi like I am than its a lot of getting used too because the visual designer of Delphi does allows this and does a very good job at it too. – GuidoG Aug 18 '15 at 15:17
  • Guido - fair enough. You might want to go back to that designer and clean those menuitems out that were added when you were trying to edit it visually. They are no longer used anywhere, but will not go away unless you manually remove them. –  Aug 18 '15 at 15:19
  • Yes I just did that, I do not like any loose ends. Thanks for your help – GuidoG Aug 18 '15 at 15:24