不知道如何解释,但是:
- 有一个控件
MyPopup
,做出来的ToolStripDropDown
; - 有很多基于
MyPopup
控件(称为弹出窗口); - 从;打开弹出窗口没有
Form
问题 - 但是从弹出窗口打开弹出窗口有问题。
问题是在子弹出窗口关闭后,即使父窗口Form
获得焦点,父弹出窗口仍保留在屏幕上。关闭卡住的父弹出窗口的唯一方法是(用鼠标)将注意力集中在它上并点击Esc.
为了让弹出窗口能够显示另一个弹出窗口,我必须欺骗Closing
事件:
void Popup_Closing(object sender, ToolStripDropDownClosingEventArgs e)
{
// prevent from closing when stay
if (_stay && e.CloseReason != ToolStripDropDownCloseReason.CloseCalled )
{
e.Cancel = true;
return;
}
}
在关闭子弹出窗口之前或之后,父弹出窗口具有:
_stay
的值为False
;- 的
Popup.AutoClose
值为True
;
我尝试使用以下内容将鼠标焦点“带回”父弹出窗口:
TopLevel=true
没运气;Focus();
没运气;Focused=true;
没运气;AutoClose=true;
没运气;Captured=true;
没运气;
还尝试将上述值设置为False
然后设置为True
,仍然没有运气。
这里还有一些代码,可能有用也可能没用:
public class MyPopup : UserControl
{
protected bool _stay = false;
private ToolStripDropDown _popup;
private ToolStripControlHost _host;
public MyPopup()
{
// create popup
_popup = new ToolStripDropDown();
_popup.Margin = _popup.Padding = Padding.Empty;
_popup.AutoSize = false;
_popup.Closing += Popup_Closing;
// add host
_host = new ToolStripControlHost(this);
_host.Margin = _host.Padding = Padding.Empty;
_host.AutoSize = false;
_popup.Items.Add(_host);
}
public void Show(Control parent, int x, int y)
{
_popup.Show(parent, x, y);
}
public new void Hide()
{
_popup.Close();
}
private void Popup_Closing(object sender, ToolStripDropDownClosingEventArgs e)
{
// prevent from closing when stay
if (_stay && e.CloseReason != ToolStripDropDownCloseReason.CloseCalled )
{
e.Cancel = true;
return;
}
}
protected void PopupChildClosedDefaultEvent(object sender, EventArgs e)
{
// hide popup if mouse is outside of client region when closing child popup
if (!ClientRectangle.Contains(PointToClient(MousePosition)))
Hide();
else
{
// >> here I am trying different things <<
_popup.AutoClose = false;
_popup.AutoClose = true;
}
}
}
public class PopupParent: MyPopup
{
private void TestChildren()
{
_stay = true;
PopupChild popup = new PopupChild();
popup.Show(button1, 0, 0);
popup.Closed += PopupChildClosedDefaultEvent;
_stay = false;
}
}
public class PopupChild: MyPopup
{
}
问题:在鼠标事件(单击客户区域外的某处)失去自动关闭能力后,有什么方法可以修复“损坏的”弹出窗口?