DaveB
DaveB

Reputation: 51

VB.NET - cannot exit For/Next loop

I'm working on a very simple time clock application for work. I have a combo box that is populated with employee names, and a list box that shows which employees are logged in. If an employee is already logged in (shows in ListIn listbox) then the sub should notify the user and exit without trying to add the person to the ListIn box again. Unfortunately, I get the "already clocked in" message but the person is added again to the ListIn box.

Here is my code:

Private Sub btnIn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIn.Click
    For i As Integer = 0 To listIn.Items.Count - 1
        If (listIn.Items(i).ToString.Contains(cboEmployees.SelectedItem)) Then
            MsgBox("Error - employee is already clocked in.", vbOKOnly, "Error")
            Exit For
        Else
            listIn.Items.Add(cboEmployees.SelectedItem)
            timerDateTime.Stop() ' Turn off the timer, prepare to display the clock-in label.
            lblTime.Text = "Success!"
            lblDate.Text = "You are clocked in."
            timerLabel.Start() ' Turn on the timer for the clock-in label.
        End If
    Next
    cboEmployees.SelectedIndex = 0 ' After clocking in, set dropdown box to blank, disables buttons again.
    listIn.Refresh()
End Sub

Any help with this little issue would be most appreciated. I am using VB.NET 2010 Professional, if that matters.

Thank you all.

Upvotes: 1

Views: 638

Answers (2)

DaveB
DaveB

Reputation: 51

The completed, working code, in case anyone else has this issue.

Private Sub btnIn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIn.Click
    Dim isIn As Boolean = False
    For i As Integer = 0 To listIn.Items.Count - 1
        If (listIn.Items(i).ToString.Contains(cboEmployees.SelectedItem)) Then
            isIn = True
            MsgBox("Error - employee is already clocked in.", vbOKOnly, "Error")
            Exit For
        End If
    Next
    If isIn = False Then
        listIn.Items.Add(cboEmployees.SelectedItem)
        timerDateTime.Stop() ' Turn off the timer, prepare to display the clock-in label.
        lblTime.Text = "Success!"
        lblDate.Text = "You are clocked in."
        timerLabel.Start() ' Turn on the timer for the clock-in label.
    End If


    cboEmployees.SelectedIndex = 0 ' After clocking in, set dropdown box to blank, disables buttons again.
    listIn.Refresh()
End Sub

Upvotes: 0

MarkNFI
MarkNFI

Reputation: 556

You're overlapping two different tasks here: searching the list and updating the list. Inside the For loop, you're testing if the current entry matches the employee, and if it doesn't match then you add the employee. That means that unless the very first entry matches the employee, you'll execute the "Else" part of your If statement, and add the employee. So even if the second entry matches, it's too late -- you already added the employee because the first entry didn't match.

What you want to do is separate the search from the update. Make a boolean variable called something like "isClockedIn" and set it to false. Then go through your For loop and if the entry matches, set isClockedIn to true and exit the loop. Then, after the For loop, do another If statement that checks isClockedIn and either updates the list or displays the error.

Upvotes: 2

Related Questions