Domanda

I need to detect whenever the MenuStrip (NOT a contextmenu) is opened and closed. The Enter and Leave events do not seem to work.

Why I need it: I use a MenuStrip with options like "File", "Edit", "About", etc. But when the menu strip is active, and the user navigates through it using either the mouse or keyboard the other controls on the Windows Form also respond to it.

For example: I click the "Edit > Paste Special.. > Unformatted text" in my application using the mouse. But below the expanded menu item is a XNA 2d-rendering control that also detects the mouse input and does something that I don't want it to do. Now if I could just detect whenever the menu is opened I could disable/enable the appropriate controls.

È stato utile?

Soluzione

Without knowing a little more about what you are doing, you can try the code below:

 private void Form1_Load(object sender, EventArgs e)
  {
     menuStrip1.GotFocus += new EventHandler(MenuStrip1_GotFocus);
     menuStrip1.LostFocus += new EventHandler(MenuStrip1_LostFocus);
  }

  private void MenuStrip1_GotFocus(object sender, EventArgs e)
  {
     textBox1.Text = "Has Focus";
  }

  private void MenuStrip1_LostFocus(object sender, EventArgs e)
  {
     textBox1.Text = "Lost Focus";
  }

  private void menuStrip1_MenuActivate(object sender, EventArgs e)
  {
     textBox1.Text = "Has Focus";
  }

  private void menuStrip1_MenuDeactivate(object sender, EventArgs e)
  {
     textBox1.Text = "Lost Focus";
  }

This seems to be working for what you have described above. If you have the menu strip with tab stop on (true) then the got focus events will handle this case. If you only need to make changes if the mouse is used then Menu Active events should work.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top