MessageBox 保持隐藏状态,直到父窗体重新获得焦点
我正在实现一个自定义控件,它提供要从外部类(即主窗体)处理的公共事件。
主窗体可以处理这些事件(在我的例子中它是一个高级 TabControl)。
我的自定义控件的摘录:
public class FlatTabControlEx : TabControl {
public delegate void OnTabCloseQueryDelegate(int tabIndex, TabPage tabPage);
public event OnTabCloseQueryDelegate TabCloseQuery;
protected override void OnPaint(PaintEventArgs e) {
DrawControl(e.Graphics);
base.OnPaint(e);
}
protected override void OnClick(EventArgs e) {
var imageRect = GetImageRectangle()
bool mouseOver = imageRect.Contains(GetMousePos());
if (mouseOver) {
if (TabCloseQuery != null) {
TabCloseQuery(i, TabPages[i]);
}
}
}
}
以下是我处理该事件的方式:
public partial class TestForm : Form {
public TestForm() {
InitializeComponent();
_flatTabControlEx.TabCloseQuery += (index, tabPage) => {
if (MessageBox.Show("Close tab with title " + tabPage.Text, "Question", MessageBoxButtons.YesNo) == DialogResult.Yes) {
_flatTabControlEx.TabPages.Remove(tabPage);
}
};
}
}
不知何故,消息框被隐藏(通过其主窗体?),并且仅在主窗体失去并重新获得焦点时才显示。提供不同的所有者似乎没有帮助。
我该如何处理这种情况以及这种行为是如何引起的?
编辑1:在上面添加了一些最小化的代码。
编辑 2:我注意到实际上是我的控件绘制在 MessageBox 上。我如何确定何时绘制它?
I'm implementing a custom control that provides public events to be handled from an outer class, i.e. the main form.
The main form can handle those events (in my case it is an advanced TabControl).
An excerpt of my custom control:
public class FlatTabControlEx : TabControl {
public delegate void OnTabCloseQueryDelegate(int tabIndex, TabPage tabPage);
public event OnTabCloseQueryDelegate TabCloseQuery;
protected override void OnPaint(PaintEventArgs e) {
DrawControl(e.Graphics);
base.OnPaint(e);
}
protected override void OnClick(EventArgs e) {
var imageRect = GetImageRectangle()
bool mouseOver = imageRect.Contains(GetMousePos());
if (mouseOver) {
if (TabCloseQuery != null) {
TabCloseQuery(i, TabPages[i]);
}
}
}
}
And here is how I handle that event:
public partial class TestForm : Form {
public TestForm() {
InitializeComponent();
_flatTabControlEx.TabCloseQuery += (index, tabPage) => {
if (MessageBox.Show("Close tab with title " + tabPage.Text, "Question", MessageBoxButtons.YesNo) == DialogResult.Yes) {
_flatTabControlEx.TabPages.Remove(tabPage);
}
};
}
}
Somehow, the messagebox gets hidden (by its main form?) and only shows up when the main form loses and regains focus. Providing a different owner didn't seem to help.
How can I handle this case and how is the behaviour caused?
Edit 1: Added some minimalized code above.
Edit 2: I noticed that it's actually my control that is drawn over the MessageBox. How can I determine when to draw it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对 datagridview cellpainting 事件也有同样的问题。
要做的就是避免重复。
当您离开控件时,会触发一些事件,例如验证、绘制等。
对于这种情况,当消息框打开时,控件会检查他的任务并开始执行。如果存在数据格式错误,则停止进程;如果存在绘制任务,则将父窗体保留在顶部,因此消息框保留在后面。
这是我的解决方案(Winforms,但应该是相同的)
I had the same problem with datagridview cellpainting event.
What to do is avoiding repetitions.
when you leave a control, some events such as validate, paint etc are triggered.
For the case, when messsage box is opened,the control reviews his tasks and start doing. İf there is a data format mistake stop process, İf there is a paint task keep the parent form on top, so message box stays behind.
This was my solution (Winforms but it should be the same)