I'm making a basic Listbox program where as you enter peoples names, press add name and it enters their name into the Listbox. I also have Clear list, Exit, and delete name buttons.
I made an IF statement, it makes it so you can only enter 5 peoples name and if you try to go over that, the 'Add name' disappears. If you clear the list the button reappears.
My problem is that if I delete a certain name, the counter resets to 0, making the user enter another 5 names overflowing the limit.
How would I make the counter set to however many names are deleted.
I made an IF statement, it makes it so you can only enter 5 peoples name and if you try to go over that, the 'Add name' disappears. If you clear the list the button reappears.
My problem is that if I delete a certain name, the counter resets to 0, making the user enter another 5 names overflowing the limit.
How would I make the counter set to however many names are deleted.
Code:
Public Class Form1
Dim counter As Integer = 0
Private Sub btnName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnName.Click
Dim First, Second As String
If counter < 5 Then
First = txtFirst.Text
Second = txtSecond.Text
lstAdd.Items.Add(First + " " + Second)
counter += 1
Else
MsgBox("You can't add more Items")
btnName.Visible = False
End If
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
lstAdd.Items.Clear()
counter = 0
btnName.Visible = True
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
Try
Dim item As String
item = lstAdd.SelectedIndex
lstAdd.Items.RemoveAt(item)
btnName.Visible = True
counter = 0
Catch ex As Exception
End Try
End Sub
End Class