2012-01-26 32 views
0

我正在实现一个自定义控件,该控件提供要从外部类(即主窗体)处理的公共事件。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); 
     } 
    }; 
    } 
} 

不知何故,在MessageBox被隐藏的(其主要形式?),只显示当主要形式失去并重新获得关注时。提供不同的所有者似乎没有帮助。

我该如何处理这种情况以及行为是如何引起的?

编辑1:添加了上面的一些简化代码。

编辑2:我注意到它实际上是通过MessageBox绘制的我的控件。我如何确定何时绘制它?

+0

叫声也像一个线程的问题。重现此行为的邮政编码。 –

+0

我没有实现任何自定义线程。我重写默认的OnPaint,可能是原因?无论如何,上面的代码已被添加。 – fjdumont

回答

0

我有与datagridview cellpainting事件相同的问题。 如何避免重复。

当您离开控件时,会触发某些事件,如验证,绘制等。

对于这种情况,当信箱打开时,控制器检查他的任务并开始做。如果存在数据格式错误停止过程,那么有一个绘制任务将父窗体保留在顶部,因此消息框保留在后面。

这是我的解决方案(的WinForms,但它应该是相同的)

Public Class HybridDataGridView 
Inherits DataGridView 
WithEvents NewDataGridViewTextBox As New TextBox 
Private NoFocus As Integer = 0 

Private Sub HybridDataGridView_LostFocus(sender As Object, e As EventArgs) Handles Me.LostFocus 
    NoFocus = 1 
End Sub 
Private Sub HybridDataGridView_GotFocus(sender As Object, e As EventArgs) Handles Me.GotFocus 
    NoFocus = 0 
    End Sub 
Private Sub HybridDataGridView_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles Me.CellPainting 
    If Me.CurrentCell Is Nothing Then 
     Exit Sub 
    End If 
    Dim Kalem As Pen 
    If e.ColumnIndex = Me.CurrentCell.ColumnIndex And e.RowIndex = Me.CurrentCell.RowIndex Then 
     If NoFocus = 0 Then 
      Kalem = New Pen(Color.Black, 1) 
      e.PaintBackground(e.ClipBounds, True) 
      e.PaintContent(e.ClipBounds) 
     End if 
     End if 

    End sub 
End Class 
+1

您能否重新解释和格式化您的答案? –

相关问题