0

I'm still working on getting a menu to display all of the input devices on a computer- pardon my third question in something that is probably very simple.

Here's the code:

List<MenuItem> inputDevice = new List<MenuItem>();
MenuItem myMenuItemInputDevices = new MenuItem("&Input Devices");
sgFileMenu.MenuItems.Add(myMenuItemInputDevice);
for (int i = 0; i < DeviceCount; i++)
{
    inputDeviceMenu.Add(new MenuItem(inputName[i]));
    myMenuItemInputDevices.MenuItems.Add(inputDeviceMenu[i]);
    myMenuItemInputDevices.Click += new System.EventHandler(this.myMenuItemInputDeviceClick);
}

This seems to work just fine, the menu items are added, everything is good, but clicks on the dropdown list are not working. I've done other work with menus, and clicks in other code are working correctly. I tried putting

myMenuItemInputDevices.Click += new System.EventHandler(this.myMenuItemInputDeviceClick);

outside of the {}, just in case that was the right way to do it, but that didn't help.

What am I missing?

3
  • I assume inputName[i] is the dropdown items that are not working? Commented Sep 4, 2013 at 19:53
  • inputName[i] is the dropdown items, but that part is working fine. Commented Sep 4, 2013 at 20:00
  • 1
    What's the difference between inputDeviceMenu and myMenuItemInputDevices? I can see only what is the second one. Commented Sep 4, 2013 at 20:02

1 Answer 1

2

You want this

List<MenuItem> inputDevice = new List<MenuItem>();
MenuItem myMenuItemInputDevices = new MenuItem("&Input Devices");
sgFileMenu.MenuItems.Add(myMenuItemInputDevice);
for (int i = 0; i < DeviceCount; i++)
{
    inputDeviceMenu.Add(new MenuItem(inputName[i]));
    inputDeviceMenu[i].Click += new System.EventHandler(this.myMenuItemInputDeviceClick);
    myMenuItemInputDevices.MenuItems.Add(inputDeviceMenu[i]);
    myMenuItemInputDevices.Click += new System.EventHandler(this.myMenuItemInputDeviceClick);
}

EDIT: It is pretty obvious that the Menu Items that you are trying to add does not have any Click event method hooked up.

    inputDeviceMenu.Add(new MenuItem(inputName[i]));

You are just adding them.

Sign up to request clarification or add additional context in comments.

2 Comments

You have the right answer but the code is probably wrong. The inputName[i] is not a MenuItem but I suppose a string
Very close- this is what works: inputDeviceMenu[i].Click += new System.EventHandler(this.myMenuItemInputDeviceClick);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.