我有一个包含以下控件的表单:
- 三个文本框(text_box_a、text_box_b、text_box_c)
- 列表框 (list_ctrl)
- 相框 (pic_frame)
我希望每当用户单击其边界之外时,列表框就会消失,但在单击一个特定的文本框 (text_box_a) 时不会消失。当它在所述文本框内单击时,它也将重新出现。
我尝试过:
Private Sub text_box_a_GotFocus()
Me.list_ctrl.Visible = True
End Sub
Private Sub list_ctrl_LostFocus()
If Not (Me.text_box_a Is Me.ActiveControl) Then
Me.list_ctrl.Visible = False
End If
End Sub
我在 Microsoft Access 2013 工作
我希望每当用户单击其边界之外时列表框就会消失......
如果您的意思是单击表单详细信息部分,通过说"在其边界之外",那么您可以使用表单的Detail_Click()
事件。
使用文本框的Enter
事件还可以"捕获"相应标签上的单击,并且在使用键盘移动焦点时也可以工作。
Private Sub Detail_Click()
If Not (Me.list_ctrl Is Me.ActiveControl) Then
Me.list_ctrl.Visible = False
End If
End Sub
Private Sub text_box_a_Enter()
Me.list_ctrl.Visible = True
End Sub
Private Sub text_box_b_Enter()
Me.list_ctrl.Visible = True
End Sub
Private Sub text_box_c_Click()
Me.list_ctrl.Visible = True
End Sub
但是,当list_ctrl
具有焦点时,隐藏它存在问题。
一种选择是在代码中使用 2 个标准函数(一个用于隐藏控件,另一个用于显示控件),然后将文本框OnGotFocus
事件分配给一个,具体取决于它是text_box_a
还是其他控件之一:
首先,设置2个不同的功能:
Public Function ShowListBox()
Me.list_ctrl.Visible = True
End Function
Public Function HideListBox()
' if active control is list box, change active control
' before hiding list box
If Me.ActiveControl.Name = Me.list_ctrl.Name Then
Me.text_box_b.SetFocus
End If
Me.list_ctrl.Visible = False
End Function
然后,可以在FormLoad
事件的循环中将不同的控制事件分配给其中一个。像这样:
Private Sub Form_Load()
' set up the form detail's OnClick event separately from the control loop.
Me.Detail.OnClick = "=HideListBox()"
Dim ctl As Access.Control
For Each ctl In Me.Controls
If ctl.Name = Me.list_ctrl.Name Then
' do nothing here
' (control will be hidden or not
' based on other control's OnGotFocus,
' or Form details OnClick
ElseIf ctl.Name = Me.text_box_a.Name Then
ctl.OnGotFocus = "=ShowListBox()"
ElseIf TypeName(ctl) = "TextBox" Then
ctl.OnGotFocus = "=HideListBox()"
End If
Next ctl
End Sub