2

到目前为止,我有一些代码允许用户点击 F1,它加载相同属性的新表单,然后隐藏他们第一个打开的表单,点击 F2,允许用户关闭新打开的表单并显示一个他们先开了。我想要一个限制,如果用户在打开 2 个相同表单的情况下按 F1,则只允许用户打开 1 个额外的表单,然后出现一个消息框,告诉他们先关闭第二个表单,否则允许打开它。

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

 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?不太确定,但我很接近。

4

1 回答 1

3

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

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

看到这个

- 编辑 -

就在我考虑的时候,要调整您的代码,您可以使用数字迭代......

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
于 2014-02-07T09:03:26.043 回答