想要只允许2个相同的表格打开VB6

时间:2014-02-07 00:02:41

标签: forms vb6 multiple-forms

到目前为止,我有一些代码允许用户点击F1加载相同属性的新形式,然后隐藏他们已经拥有的第一个,点击F2,允许用户关闭新打开的表单,展示他们首先打开的那个。我想要一个限制,允许用户只打开一个额外的表格,如果他们用两个相同的表格打开F1然后出现一个消息框告诉他们先关闭第二个表单,否则允许它打开。

这是我到目前为止所拥有的。

 Private Sub Form_Load()

 End Sub

 Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

    Select Case KeyCode

        Case vbKeyF1
        'hides the current form
            Me.Hide
        'loads a new form with the same properties
            Dim f As New Form1
            Load f
        'shows this new form
            f.Show
        'says that the second form is open
            fOpen = True

        Case vbKeyF2
        'closes the second form
            Unload Me
        'says that the second form is closed
            fOpen = False
        'shows the first form you were on
            Form1.Show

    End Select
End Sub

Private Sub Form_QueryUnload(cancel As Integer, unloadmode As Integer)

   'if your hitting "X" on second form then just close form2
   If fOpen = False Then
   Form1.Show
   Else
   'if your hitting "X" on main form close everything
   Unload Me
   End If

End Sub

也许像fOpen = true那样不允许用户点击F1?不太确定,但我很接近。

1 个答案:

答案 0 :(得分:3)

请原谅我,如果我的VB6有点关闭,但您需要枚举Forms集合,以检查您的表单是否已经打开...

Dim frm As Form
For Each frm In Forms
    If frm.Name = "myForm" Then frm.Show()
Next frm

请参阅this

- 编辑 -

在我考虑的时候,为了调整你的代码,你可以使用数字迭代......

Dim f As Integer
Dim t As Integer
t = Forms.Count - 1
For f = 0 To t
    If Forms(f).Name = "myForm" Then Forms(f).Show()
Next frm

- 编辑2 -

再一点说明这一点。您可能还想介绍一个计数器,以便检查原始帖子中是否有两个字段...

Dim frm As Form
Dim c As Integer
For Each frm In Forms
    If frm.Name = "myForm" Then 
        c = c + 1
        If c = 2 Then 
            frm.Show()
            Exit For 'Speed up the search if there are lots of forms
        End If
    End if
Next frm