检查是否有任何对话框打开

时间:2012-11-12 13:20:42

标签: vb.net winforms

有人在这看到我的错误吗?

我无法识别表单是否在我的应用中显示为对话框。

Public Class Form1

  Private m As Form2

  Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
    Me.Text = DateTime.Now.ToLongTimeString & " " & IsAnyDialogShown()
  End Sub

  Public Function IsAnyDialogShown() As Boolean
    For Each f As Form In Me.OwnedForms
       If f.Owner Is Me Then
         Return True
       End If
    Next
  End Function

  Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    m = New Form2
    m.ShowDialog()
  End Sub

End Class

3 个答案:

答案 0 :(得分:1)

您正在寻找的是物业模式。

检查表单的模态属性是否为true(这意味着表单显示为ShowDialog)

For Each f As Form In Me.OwnedForms 
   If  f.Modal=True Then 
     'your code here
   End If
Next

现在你的错误(我现在没有Visual Studio可以尝试)但是你的IsAnyDialogShown(),似乎它总是返回true:

For Each f As Form In Me.OwnedForms ' (So f belongs to Me)
   If f.Owner Is Me Then 'f.Owner is always me because you are seaching in forms that have as owner the Me form
     Return True
   End If
Next
希望我帮了一下。 告诉我,我是否可以做更多的事情

所以在你发表评论之后 试试这个:

        For Each frm as Form In Application.OpenForms
            If frm.Modal=True Then
            'do something 
            'Actually you should have only one form because only one can be in modal state
            end if
        Next

答案 1 :(得分:0)

您需要检查表单的Visible属性,即布尔值。 如果是,则显示表单,否则隐藏。

答案 2 :(得分:0)

那只是做我拥有的表格。与它们是否是对话形式无关。 即它会选择正常形式。

此外,如果您希望此功能按预期工作,则应使用传递所有者的重载。

m.ShowDialog(Me);

一样

不是我做过的事情,但如果老板不是我的话。我想要我的钱。