Hello all,

VB6 newbie here. I've got a basic form containing a list box, with two command buttons; one to add items to the list, and one to remove items.

The two command buttons work fine, but I want to disable the "Add" button once there are 8 items in the list, and disable the "Remove" button if the list is empty.

Here's my code so far;

Private Sub cmdAddItem_Click()
Static count As Integer
Static name As String
count = lstPoints.ListCount
count = count + 1
name = "Point " & count
lstPoints.AddItem name
End Sub

Private Sub cmdRemoveItem_Click()
Size = lstPoints.ListCount - 1
lstPoints.RemoveItem Size
End Sub

I've tried a number of things myself, including an if statement to disable the cmdAddItem button if the size of the list is greater than 8. Also tried a while loop (while size of list is less than 8, enable button, otherwise disable button) but that added all 8 items in the list at the same time.

Any ideas? Thanks

Recommended Answers

All 5 Replies

did you try

If lstPoints.ListCount = 8 Then
cmdremoveitem.Enabled = False
cmdadditem.Enabled = False
End If

?

did you try

If lstPoints.ListCount = 8 Then
cmdremoveitem.Enabled = False
cmdadditem.Enabled = False
End If

?

Yes I tried this; what happens is that the command button gets disabled when there are 8 items in the list - which is great!
But when I remove items, the "AddItem" box is still disabled. What I want is for the add command button to ONLY be disabled if there are 8 items, and the remove button to only be disabled when there are no items in the list.

Try this :

Private Sub cmdAddItem_Click()
Static count As Integer
Static name As String
count = lstPoints.ListCount
count = count + 1
name = "Point " & count
lstPoints.AddItem name

If count = 8 Then
    cmdAddItem.Enabled = False
    cmdRemoveItem.Enabled = True
End If
End Sub

Private Sub cmdRemoveItem_Click()
Size = lstPoints.ListCount - 1
lstPoints.RemoveItem Size

If lstPoints.ListCount = 0 Then
    cmdRemoveItem.Enabled = False
    cmdAddItem.Enabled = True
End If
End Sub

'or try this one I just modify Jx_Man Code.

Private Sub cmdAddItem_Click()
Static count As Integer
Static name As String
count = lstPoints.ListCount
count = count + 1
name = "Point " & count
lstPoints.AddItem name

If count = 8 Then
    cmdAddItem.Enabled = False
End If
'when we add an item we set cmdRemoveItem to enable
cmdRemoveItem.Enabled =true
End Sub

Private Sub cmdRemoveItem_Click()
Size = lstPoints.ListCount - 1
lstPoints.RemoveItem Size

If lstPoints.ListCount = 0 Then
    cmdRemoveItem.Enabled = False
End If
'we enable cmdAddItem
cmdAddItem.Enabled = true
End Sub

Thanks alot guys, problem solved!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.