0

Solution found here: How can I customize the system menu of a Windows Form?. Anyway, thanks for your help :)







I want to add a new item to the default contextmenu of a form, which appears when right clicked on the top bar (where the minimize box and maximize box are).

I'd also prefer to to it programatically (not in the designer)

I've tried this

public Form1()
{
    InitializeComponent();
    this.ContextMenu.Add(new MenuItem("Test")); //->NullReferenceException
    this.ContextMenu = new ContextMenu(/*..*/); //-> Not what I want
}

which leads to a NullReferenceException. if I set this.ContextMenu it affects only the contextmenu that appears when you right click into the form, which I don't need in this case.

I hope someone can help me out ^^ I know it needs to be possible somehow since I've seen it in a lot of Programs already

Community
  • 1
  • 1
Stefan
  • 652
  • 5
  • 10

2 Answers2

0

Here's one example by Microsoft:

public partial class TextBoxContextMenuDemo : Form
{
    ContextMenu mnuContextDefault;
    ContextMenu mnuContextAlt;

    MenuItem mnuItmAltMenuTest;

    public TextBoxContextMenuDemo()
    {
        InitializeComponent();
        InitializeAltContextMenu();
    }

    private void InitializeAltContextMenu()
    {
        mnuContextDefault = new ContextMenu();
        mnuContextDefault = this.TextBox1.ContextMenu;

        mnuItmAltMenuTest = new MenuItem();
        mnuItmAltMenuTest.Index = -1;
        mnuItmAltMenuTest.Text = "Test Menu Item";
        mnuItmAltMenuTest.Click += new System.EventHandler(this.mnuItmAltMenuTest_Click);

        mnuContextAlt = new ContextMenu();
        mnuContextAlt.MenuItems.Add(mnuItmAltMenuTest);
    }

    private void TextBox1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            if ((Control.ModifierKeys == Keys.Control))
            {
                this.TextBox1.ContextMenu = mnuContextAlt;
                TextBox1.ContextMenu.Show(TextBox1, new Point(e.X, e.Y));
            }
            else
            {
                this.TextBox1.ContextMenu = mnuContextDefault;
            }
        }
    }

    private void mnuItmAltMenuTest_Click(object sender, System.EventArgs e)
    {
        MessageBox.Show("You clicked the alternate test menu item!");
    }
}
Abhishek Dey
  • 1,601
  • 1
  • 15
  • 38
  • Thank you for the quick reply :) I've tested it, but is's not working for me. It throws a NullReferenceException right at `mnuContextDefault = this.ContextMenu;`, which is actually my problem. How do I acces that one? Like if you right click on the top left corner of a form – Stefan Sep 09 '15 at 11:57
0

may be these help

http://www.c-sharpcorner.com/UploadFile/deepak.sharma00/how-to-customize-default-contextmenu-of-a-textbox-control-in/

Is it possible to obtain and modify standard system context menu for textbox?

Community
  • 1
  • 1
Ramankingdom
  • 1,478
  • 2
  • 12
  • 17
  • Unfortunately, this also only affects the Contextmenu when I right click the Form. I edited my question, I found a solution that fits :) – Stefan Sep 09 '15 at 13:36