Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7623159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:38:14+00:00 2026-05-31T04:38:14+00:00

I am adding Context Items for a Context Menu and showing the required Items

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T04:38:16+00:00Added an answer on May 31, 2026 at 4:38 am

    This (very basic code) should do it. Place following code in the constructor of your Form:

    var contextMenu = new ContextMenu();
    contextMenu.MenuItems.Add(new MenuItem("New"));
    contextMenu.MenuItems.Add(new MenuItem("Close"));
    this.ContextMenu = contextMenu;
    

    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:

    var contextMenu = new ContextMenu();
    
    var itemOne = new MenuItem("New");
    itemOne.Click += ContextMenuItemClick;
    contextMenu.MenuItems.Add(itemOne);
    
    var itemTwo = new MenuItem("Close");
    itemTwo.Click += ContextMenuItemClick;    
    contextMenu.MenuItems.Add(itemTwo);
    
    ContextMenu = contextMenu;
    

    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:

    private void ContextMenuItemClick(object sender, EventArgs e)
    {
        var selectedItem = (MenuItem)sender;
        switch(selectedItem.Text)
        {
            case "New" : //do some new stuff
                break;
            case "Close": //do some closing stuff
                break;
        }
    }
    

    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:

    itemOne.Click += itemOne_Click;
    itemTwo.Click += itemTwo_Click;
    
    private void itemOne_Click(object sender, EventArgs e)
    {
        //do the new stuff
    }
    
    private void itemTwo_Click(object sender, EventArgs e)
    {
        //do the closing stuff
    }
    

    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:

    var itemOne = new MenuItem("New") { Name = "NewItem" };
    var itemTwo = new MenuItem("Close") { Name = "CloseItem" };
    

    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:

    contextMenu.MenuItems["NewItem"].Index = 1;
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm programmatically adding ToolStripButton items to a context menu. That part is easy. this.tsmiDelete.DropDownItems.Add(The
I have read these: Adding item to the Desktop context menu in Windows http://www.informit.com/articles/article.aspx?p=169474
How would I go about adding a context menu to a spinner popup? I
Custom values to Context Menu Items in JQgrid contains great sample about adding context
I'm adding a custom context menu item to documents (and not folders) in a
I can create a menu item in the Windows Explorer context menu by adding
How would one go about adding a submenu item to the windows explorer context
I created a label using the new label context menu: I defined the parameters
I have a context menu with a few items. One of the items has
I have a context menu on a TextView representing a user name in my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.