我遇到列表框在Excel表单上的行为方式存在问题。重现问题的步骤:
在此用户表单中使用以下代码:
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Me.ListBox1.Locked = True
Me.ListBox1.Locked = False
End Sub
Private Sub UserForm_Initialize()
Dim i As Integer
For i = 1 To 10
Me.ListBox1.AddItem i
Next i
End Sub
首次显示表单时,我可以使用箭头键和页面键正常导航列表框。但是,在触发双击事件后,所有键盘导航都不起作用,包括跳转到其他控件(如果它们在表单上)。单击列表框似乎确实有效,并且焦点轮廓正确显示,但在列表框被锁定然后解锁后处理焦点的方式有问题。发生了什么事?
使用Office 2013 32位版本。
答案 0 :(得分:1)
我设法复制了这个问题,并在锁定和解锁列表框之前将焦点设置在其他地方为我工作:
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Me.TextBox1.SetFocus 'or some other control.
Me.ListBox1.Locked = True
Me.ListBox1.Locked = False
Me.ListBox1.SetFocus
End Sub
答案 1 :(得分:0)
我测试的2个解决方案允许您使用箭头键进行导航。
鉴于没有单击事件处理程序,请尝试在DblClick之后调用单击事件(始终有效):
Private Sub ListBox1_Click()
Debug.Print "ListBox1_Click()"
End Sub
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Debug.Print "ListBox1_DblClick()"
With Me.ListBox1
.Locked = True
.Locked = False
End With
ListBox1_Click
End Sub
Private Sub UserForm_Initialize()
Dim i As Integer
For i = 1 To 10
Me.ListBox1.AddItem i
Next i
End Sub
在DblClick结束时设置Cancel = False
。 (有时候不起作用!)
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Debug.Print "ListBox1_DblClick()"
With Me.ListBox1
.Locked = True
.Locked = False
End With
Cancel = False
End Sub
Private Sub UserForm_Initialize()
Dim i As Integer
For i = 1 To 10
Me.ListBox1.AddItem i
Next i
End Sub
答案 2 :(得分:0)
在具有列表框的工作表中更改缩放对我有用。
Private Sub Worksheet_Activate()
Dim temp As Double
Application.ScreenUpdating = False
'Change worksheet zoom setting for the active window
temp = ActiveWindow.Zoom
ActiveWindow.Zoom = temp + 10
ActiveWindow.Zoom = temp
Application.ScreenUpdating = True
End Sub