访问VBA列表框方法.Selected与.ItemData [英] Access VBA listbox method .Selected versus .ItemData
问题描述
在处理应用程序时,我无法在列表框中获取选定的项以实际输出正确的行值.我正在使用.Selected
方法选择需要选择的行以进行下一步,这将从列表框中拉出绑定列的值以进行进一步处理,而无需用户与列表框进行交互
While working on an application I had trouble getting a selected item in my listbox to actually output the correct row value. I was using the .Selected
method to choose which row needed to be selected in order to proceed to the next step, which would pull the bound column's value from the listbox for further processing, without the need of the user to interact with the listbox.
事实证明,.Selected
与单击列表框行的方式不同,因为其输出值仍然是最后手动"选择的行的绑定列值.
It turns out that .Selected
doesn't work the same way as clicking on a listbox row, as its output value remains the last "manually" selected row's bound column value.
我意识到我必须用.ItemData
方法来跟上.Selected
方法,才能使列表框的输出值等于我在.Selected
方法中选择的行.
I realized I had to follow up the .Selected
method with a .ItemData
method to actually have the listbox output value to equal the row that I selected in the .Selected
method.
这是我用来将供应商PN查找"表中的选择内容复制到主要零件搜索"表中的代码:
Here is the code I used to copy selections on a Vendor PN Lookup Form to a Main Part Search Form:
Private Sub cmdAddTofrmPartSearch_Click()
Dim i as Integer
... [truncated]
Forms("frmPartSearch").lstSearchResults.Selected(i - 1) = True
... [truncated]
Forms("frmPartSearch").txtHiddenPN.Value
= Forms("frmPartSearch").lstSearchResults.ItemData(i - 1)
...[truncated]
End Sub
如上所述,为什么我需要使用:
As above, why do I need to use:
Forms("frmPartSearch").lstSearchResults.Selected(i - 1) = True
AND
Forms("frmPartSearch").txtHiddenPN.Value
= Forms("frmPartSearch").lstSearchResults.ItemData(i - 1)
要让txtHiddenPN
接收正确的行值,而不是简单地将txtHiddenPN
的控制源设置为=[lstSearchResults]
,并将其更新为.Selected
之后的行值?
To get txtHiddenPN
to receive the correct row value instead of simply setting txtHiddenPN
's control source to =[lstSearchResults]
and have it update to the row value after .Selected
?
.Selected
的用途是什么(如果不更改列表框的输出值(不方便,而是徒劳地突出显示了一行))?同样,为什么.ItemData
也不突出显示其绑定值正在输出的行?这是不好的设计,还是有另一种方法可以同时达到两个结果?最佳实践是顺序自动包括这两种方法吗?是否存在只需要调用一种方法的情况?
What use is .Selected
if it doesn't change the output value of the listbox (other than handily, but futilely highlighting a row)? And similarly, why doesn't .ItemData
also highlight the row whose bound value is being outputted? Is this bad design, or is there another method that achieves both results simultaneously? Is it best practice to automatically include both methods sequentially? Are there situations where only one method needs to be called?
我是VBA和编码的新手,所以也许这是该领域的普遍现象,但似乎有点钝.
I'm new to VBA and coding in general, so maybe this is a common occurrence in the field, but it just seems obtuse.
推荐答案
选定的" ListBox属性不会影响值"
"Selected" ListBox property doesn't affect "Value" one
它甚至不会触发事件
选定"属性主要用于检查选择了哪些列表框元素
"Selected" property is mainly used to check which listbox elements is selected
您可能要选中此
因此您不需要Forms("frmPartSearch").lstSearchResults.Selected(i - 1) = True
代码行,因为它对您的代码没有影响
so you wouldn't need Forms("frmPartSearch").lstSearchResults.Selected(i - 1) = True
codeline since it has no effect to your code
要想更全面地了解列表框的功能,您可能还需要按照以下说明进行操作
to have a more thorough knowledge of listbox functioning you may also want to play with it as follows
将此代码添加到您项目的模块中
add this code in a module of your Project
Option Explicit
Sub PlayWithListBox()
With UserForm4
With .ListBox1
.AddItem "a1"
.AddItem "a2"
.AddItem "a3"
.AddItem "a4"
End With
.Show
MsgBox "going to use Select property"
With .ListBox1
.Selected(2) = True
Call ShowValueAndListIndex(.value, .List(.ListIndex))
End With
End With
Unload UserForm4
End Sub
Sub ShowValueAndListIndex(valueStrng As String, listIndexStr As String)
MsgBox "Value: " & valueStrng
MsgBox "listIndex: " & listIndexStr
End Sub
然后添加到您的项目
- 以"UserForm4"命名的用户表单
- 以"Userform4"中的"ListBox1"命名的ListBox
最后在"UserForm4"代码模块中添加以下代码
and finally add the following code in "UserForm4" code module
Private Sub ListBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
MsgBox "BeforeUpdate"
With Me.ListBox1
If .ListIndex >= 0 Then Call ShowValueAndListIndex(.value, .List(.ListIndex))
End With
End Sub
Private Sub ListBox1_Change()
MsgBox "Change"
With Me.ListBox1
If .ListIndex >= 0 Then Call ShowValueAndListIndex(.value, .List(.ListIndex))
End With
End Sub
Private Sub ListBox1_Click()
MsgBox "Click"
With Me.ListBox1
If .ListIndex >= 0 Then Call ShowValueAndListIndex(.value, .List(.ListIndex))
End With
End Sub
Private Sub ListBox1_Enter()
MsgBox "Enter"
With Me.ListBox1
If .ListIndex >= 0 Then Call ShowValueAndListIndex(.value, .List(.ListIndex))
End With
End Sub
Private Sub ListBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
MsgBox "KeyDown"
With Me.ListBox1
If .ListIndex >= 0 Then Call ShowValueAndListIndex(.value, .List(.ListIndex))
End With
End Sub
Private Sub ListBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
MsgBox "MouseDown"
With Me.ListBox1
If .ListIndex >= 0 Then Call ShowValueAndListIndex(.value, .List(.ListIndex))
End With
End Sub
这篇关于访问VBA列表框方法.Selected与.ItemData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!