2016-02-26 68 views
1

在應用程序上工作時,我無法在列表框中獲取選定項目以實際輸出正確的行值。我正在使用.Selected方法來選擇需要選擇哪一行,以便繼續下一步,這將從列表框中拉出綁定列的值以供進一步處理,而無需用戶與列表框進行交互。訪問VBA列表框方法。選中與.ItemData

事實證明,.Selected與單擊列表框行不一樣,因爲它的輸出值仍然是最後一個「手動」選定行的綁定列值。

我意識到我必須採用.ItemData方法跟進.Selected方法,才能將列表框輸出值與我在.Selected方法中選擇的行相等。

這裏是我以前賣方PN查找表上覆制選擇到的主要部分搜索表單代碼:

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 

如上,爲什麼我需要使用:

Forms("frmPartSearch").lstSearchResults.Selected(i - 1) = True 

AND

Forms("frmPartSearch").txtHiddenPN.Value 
    = Forms("frmPartSearch").lstSearchResults.ItemData(i - 1) 

要獲得txtHiddenPN以接收正確的行值而不是簡單地settintin g txtHiddenPN的控制源到=[lstSearchResults]並且在.Selected之後讓它更新爲行值?

.Selected如果它不改變列表框的輸出值(除了手工操作,但是無意突出顯示一行),有什麼用?同樣,.ItemData爲什麼不突出顯示正在輸出界限值的行?這是不好的設計,還是有另一種方法可以同時實現兩個結果?自動包含兩種方法是否是最佳做法?是否有隻需要調用一種方法的情況?

我是新來的VBA和編碼的一般,所以也許這是在該領域經常發生的事情,但它只是似乎鈍。

回答

1

「選定」列表框屬性不會影響「價值」一

它甚至不觸發事件

「選中」屬性主要是用來檢查哪些元素列表框中選擇

你可能想check this

這樣你就不需要Forms("frmPartSearch").lstSearchResults.Selected(i - 1) = True代碼行,因爲它要你的代碼沒有任何影響

有列表框的更透徹的瞭解發揮作用,你也可能想用它玩如下

您的項目

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」
  • 一個 「ListBox1的」 命名的列表框在 「Userform4」
命名用戶窗體

最後添加以下代碼在「UserForm4」代碼模塊

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