Question

I have a spin button and a two list boxes. i have set the min and max of spin button. I already have a button which moves data from list box1 to listbox2.But i want to set a limit for moving the data based on spin button. example: if value in spin button is 2, then only 2 items can be added to listbox2

note:i have linked the spin button to a textbox

Was it helpful?

Solution

Declare a variable and then every time you move an item, increment the value of the variable by 1. So next time when you click the button to move the item, simply compare the value of the variable with the SpinButton's Value. For example (Untested)

Dim nMoved As Long

Private Sub CommandButton1_Click()
    If nMoved < (VAL(TextBox1.Text) + 1)
        '
        '~~> Code to move items from LB1 to LB2
        '

        nMoved = nMoved + 1
    Else
        MsgBox "Max items that can be moved from LB1 to LB2 reached."
    End If
End Sub

FollowUp from Comments

You need to use .ListCount in your case only if you are matching the items count in the LB against the TB. What I understood from your post is a user shouldn't be able to move more items into the LB than what is specified in the TB. Anyways, it seems that your query is sorted.

Regarding your 2nd comment.

You need to loop through the LB items using the code below and then exit the loop if the counter is equal to the TB value. For example

For i = 0 To (ListBox1.ListCount - 1)
    If i = (Val(TextBox1.Text) - 1) Then Exit For
        '
        '~~> Code to move items from LB1 to LB2
        '
    End If
Next i

OTHER TIPS

Thanks you all.. finally i got the answer

Dim ct As Integer
Dim ictr As Long
Dim jctr As Integer
jctr = CInt(Me.TextBox1.Value)
If jctr = ListBox2.ListCount Then
   MsgBox "Maximum limit has been reached. You cannot add more players"
    Exit Sub
End If
  For ictr = jctr To 1 Step -1
    Me.ListBox2.AddItem Me.ListBox1.List(ictr)
Me.ListBox1.RemoveItem ictr
Next ictr
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top