2014-05-21 97 views
0

我有一個列表框包含10次。我有UP和DOWN按鈕來上下移動項目。我的VBA只在我將listbox multiselect屬性設置爲'None'時才起作用。對於多選=簡單的選擇是在有效使用空的拋出錯誤,如在這行代碼如何將列表框中的項目向上和向下移動訪問VBA?

 sText = lbfNames.Column(0, iIndex) 

我VBA

Private Sub cmdUP_Click() 
    Dim sText As String 
     Dim iIndex As Integer 
     iIndex = lbfNames.ListIndex 
     'check: only proceed if there is a selected item 
     If lbfNames.ListCount > 1 Then 
     'index 0 is top item which can't be moved up! 
     If iIndex <= 0 Then 
      MsgBox ("Can not move the item up any higher.") 
      Exit Sub 
     End If 
     ' If iIndex = -1 Or lbfNames.ListCount > 1 Then 
     'save items text and items indexvalue 
     sText = lbfNames.Column(0, iIndex) 
     lbfNames.RemoveItem iIndex 
     'place item back on new position 
     lbfNames.AddItem sText, iIndex - 1 
     'if you keep that item selected 
     'you can keep moving it by pressing cmdUp 
     lbfNames.Selected(iIndex - 1) = True 
     iIndex = iIndex - 1 
    End If 
    End sub 

,我是嘗試轉換下面的C#代碼(在計算器中發現)訪問VBA引發錯誤。一些數據成員未找到。

 public void MoveUp() 
{ 
    MoveItem(-1); 
} 

public void MoveDown() 
{ 
    MoveItem(1); 
} 

public void MoveItem(int direction) 
{ 
    // Checking selected item 
    if (listBox1.SelectedItem == null || listBox1.SelectedIndex < 0) 
     return; // No selected item - nothing to do 

    // Calculate new index using move direction 
    int newIndex = listBox1.SelectedIndex + direction; 

    // Checking bounds of the range 
    if (newIndex < 0 || newIndex >= listBox1.Items.Count) 
     return; // Index out of range - nothing to do 

    object selected = listBox1.SelectedItem; 

    // Removing removable element 
    listBox1.Items.Remove(selected); 
    // Insert it in new position 
    listBox1.Items.Insert(newIndex, selected); 
    // Restore selection 
    listBox1.SetSelected(newIndex, true); 
} 

是否有無論如何要做到這一點在access vba。

回答

0

我實際上重建了這個設置,但是永遠不會得到你提到的錯誤。我確實玩弄了代碼,將其調整爲您正在嘗試做的事情。試試這個:

Private Sub cmdup_Click() 
Dim sText As String 
Dim iIndex As Variant 
Dim selection() As Integer 
Dim n, topSelection As Integer 

' save the indexes of the selected items, 
' they will be deselected after the first removal 
For Each iIndex In lbfnames.ItemsSelected 
    ReDim Preserve selection(0 To n) 
    selection(n) = iIndex 
    n = n + 1 
Next 

'loop through all the selected indexes 
'this will also ensure you will only proceed if there is a selected item 
For n = LBound(selection) To UBound(selection) 
    'save items text and items indexvalue 
    sText = lbfnames.Column(0, selection(n)) 

    If selection(n) <= topSelection Then 'index topSelection is top item which can't be moved up! 
     MsgBox ("Can not move item '" & sText & "' up any higher.") 
     topSelection = topSelection + 1 
    Else 
     'first remove item from old position 
     lbfnames.RemoveItem selection(n) 
     'place item back on new position 
     lbfnames.AddItem sText, selection(n) - 1 
     'change the index of the selected value to the new index (for reselection) 
     selection(n) = selection(n) - 1 
    End If 
Next 
'loop through the selection again to reselect 
For n = LBound(selection) To UBound(selection) 
    lbfnames.Selected(selection(n)) = True 
Next 
End Sub 

代碼和註釋是不言自明,我認爲,但這裏是一個快速運行通過:

  1. 我第一次保存所選元素的索引,因爲我中取出後,發現 /添加了選擇爲 的元素。
  2. 然後我運行這個選擇,我在這裏重用你的代碼。 更改了彈出消息的條件,因爲如果您選擇 例如前兩個元素(比如1和2),則不希望只有 獲取1的消息框,然後在下一個循環中將2提前爲1. (除非你想要,那麼改變這個條件回0)
  3. 添加結束I循環通過選定的元素第二次再次選擇它們將它們進一步上移到列表中。

注意:示例C#代碼顯示了一個更通用的移動方向函數。我沒有適應這一點,我認爲這是一個好主意,但讓你去實現(總是一個很好的練習來理解代碼)。

+0

作品完美!!非常感謝。對於項目向下移動,我是否需要更改* Else *部分中的代碼? – sam

+0

@sam確實,Else部分中的代碼以及topSelection變量(因爲現在底部的項目是限制)。最簡單的方法就是嘗試調整向下移動的功能,並在與此功能比較時看到如何將它們組合成一個更通用的功能(如C#示例)。嘗試一下,玩一玩,當你陷入困境時只是問一個新問題。 – Yoh

相關問題