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

Dani AI

Generated

Nice work getting this sorted, . , and were right to toggle the buttons by list size. A small improvement is to centralize that logic so you never forget to re-enable a button after a removal. Put the enable/disable rules in one routine and call it whenever the list changes and on form initialization.

Private Sub UpdateButtons()
    Const MAX_POINTS As Integer = 8
    cmdAddItem.Enabled = (lstPoints.ListCount < MAX_POINTS)
    cmdRemoveItem.Enabled = (lstPoints.ListCount > 0)
End Sub

Call UpdateButtons at the end of your Add and Remove handlers and from Form_Load. Also, always guard removal operations so you do not call RemoveItem when the list is empty (or check ListIndex if you remove the selected item). Using the MAX_POINTS constant keeps the limit easy to change, and a short UX hint (message box or tooltip) when the add limit is reached helps clarify behavior for users.

This keeps code DRY, reduces bugs where a button stays disabled, and makes the logic easier to maintain if you later change how items are added or removed.

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.