954,551 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Disable command button

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

raheel88
Newbie Poster
12 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

did you try

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

?

yeeitsneo
Newbie Poster
22 posts since Sep 2011
Reputation Points: 10
Solved Threads: 1
 

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.

raheel88
Newbie Poster
12 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

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
Jx_Man
Nearly a Senior Poster
3,329 posts since Nov 2007
Reputation Points: 1,372
Solved Threads: 444
 

'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
Cruize_Invades
Light Poster
40 posts since May 2007
Reputation Points: 6
Solved Threads: 7
 

Thanks alot guys, problem solved!

raheel88
Newbie Poster
12 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: