Panel.Dispose closes context menu, why?

307 Views Asked by At

As per Cody's answer to this question, I derived a class from ContextMenuStrip and handle the ProcessCmdKey. This successfully prevents the menu from closing when pressing Enter while a textbox embedded inside the menu has focus. So all is well except for one thing.

Upon the Enter press, I remove the Textbox from the menu, and disposing this control causes the menu to close. When I do this:

If (m_inputPanel IsNot Nothing) Then m_inputPanel.Hide()
If (m_inputPanel IsNot Nothing) Then m_inputPanel.Parent.Controls.Remove(m_inputPanel)
If (m_inputPanel IsNot Nothing) Then m_inputPanel.Dispose()

the menu is closed. When I comment out the third line, the menu remains open. (Incidentally, m_inputPanel.Parent is a custom control which is embedded inside the menu using a ToolStripControlHost)

Why on God's Green Earth does it matter that I dispose a control which is no longer part of the menu?

1

There are 1 best solutions below

3
On

You are removing focus from the context menu when you do this, since it was the textbox that had focus when you removed it now there is nothing in the ContextMenuStrip that has focus any more.