I am adding Context Items for a Context Menu and showing the required Items where necessary for the user based on user selection. I would like to show these context items for the user NEW and CLOSE..
I did some thing like
ContextMenu.Add(NEW)
ContextMenu.Add(CLOSE)
But I am getting this in sorted order like CLOSE first and NEW last . But I need to display NEW first and CLOSE last. Is it possible to do.
This (very basic code) should do it. Place following code in the constructor of your Form:
Note: you still have to add the events yourself… 🙂
Update:
To add events to the items you’ll have to declare them in a variable instead of passing them directly in the Add() method for the MenuItems. So the previous code will look like this:
As you can see, bot items have the same eventhandler for the Click-event. In that event you check which item was clicked. That code looks like this:
Note that you could also set a separate eventhandler for each item in the menu, but then you end up with lots of methods for basically the same stuff… 🙂 Hope this helps!
Update2:
With all the help I gave, you normally should have been able to achieve this by yourself, not? 🙂 Anyway, for a separate handler the code will look like this:
Update3:
If you gave proper names to the menuitems you already added, you can change the order using the Index-property. Say I have following menuitems added to a ContextMenu:
The ‘NewItem’ will be the first item and the ‘CloseItem’ will be the second. Now if I want to change the order without touching previous code you can do this:
This will set the ‘CloseItem’ as the first and the ‘NewItem’ as the second. If you have more than 2 items, you better set the Index-property for each item individually.