Problem to show menu

HuaMin Chen
HuaMin Chen used Ask the Experts™
on
Hi,
To the codes like
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public void AddMenu()
        {
            MainMenu mnuFileMenu = new MainMenu();
            this.Menu = mnuFileMenu;
            System.Windows.Forms.MenuItem myMenuItemFile = new System.Windows.Forms.MenuItem("&File");
            System.Windows.Forms.MenuItem myMenuItemNew = new System.Windows.Forms.MenuItem("&New");
            mnuFileMenu.MenuItems.Add(myMenuItemFile);
            mnuFileMenu.MenuItems.Add(myMenuItemNew);
        }
        private void Form1_Load(object sender, System.EventArgs e)
        {
            AddMenu();
        }
    }
}

Open in new window

why can't the menu be shown to the form, by this project below?
https://app.box.com/s/oxsbqcr0kd72e4y3ww8ugogigoroibx0
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
This code is working fine at my side. Showing two menuitems File and New.

Untitled.png
Check that Load event is firing at your side or not.

If page load event is not fired then check that event is registered in Form1.Designer.cs
this.Load += new System.EventHandler(this.Form1_Load);

Open in new window

HuaMin ChenProblem resolver

Author

Commented:
Sorry, I want to add more menu-item by
            ContextMenu m = new ContextMenu();
            MenuItem addDevice = new MenuItem("Add Device");
            MenuItem deleteDevice = new MenuItem("delete Device");
            MenuItem fire = new MenuItem("Fire");
            MenuItem fault = new MenuItem("Fault");
            MenuItem suppress = new MenuItem("Suppress");
            m.MenuItems.AddRange(new MenuItem[] { addDevice, deleteDevice, fire, fault, suppress });
            Form1.ContextMenu = m;
            m.Show((Control)(this), e.Location);

Open in new window


how to correct these?
Error      1      An object reference is required for the non-static field, method, or property 'System.Windows.Forms.Control.ContextMenu.get'      34      13      WindowsFormsApplication2
Error      2      The name 'e' does not exist in the current context      35      37      WindowsFormsApplication2
Commented:
You have pasted code to add context menu on the form.

Replace Form1 with this keyword.
this.ContextMenu = m;

Open in new window


Set the position of your context menu

m.Show((Control)(this),Cursor.Position);
or
any specific position
m.Show((Control)(this),new Point(20,30));

Open in new window

HuaMin ChenProblem resolver

Author

Commented:
How to correct

Error	1	The name 'e' does not exist in the current context	

Open in new window

due to last line below

            ...
            m.MenuItems.AddRange(new MenuItem[] { addDevice, deleteDevice, fire, fault, suppress });
            this.ContextMenu = m;
            m.Show((Control)(this), e.Location);

Open in new window

Commented:
There is no need to call show() function. By calling this function, context menu will always be visible.

You can comment out this line and try. When you right click on the form, that context menu will be visible automatically.

Instead you can remove this error by writing

m.Show((Control)(this),Cursor.Position);

For any specific position
m.Show((Control)(this),new Point(20,30));

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial