-2

I was actually working an Attendance System, using listbox i want to monitor whether "employees" timed in or out. txtEmpNum.text as my textbox, rdTin as my radio button for time in, rdTout as my radio button for time out, lblName, lblDept, lblinout are just label. I want that if a user already timed in his/her name wont appear on my listbox rather msgbox pop up. But on this code although msgbox poped up, still the name of the employee appears on my listbox.

If txtEmpNum.Text = 8888 Then
        If rdTin.Checked = True Then
            For i As Integer = 0 To listEmp.Items.Count - 1
                If (listEmp.Items(i).ToString.Contains("Bane Lim")) Then
                    MsgBox("String found at " & (i + 1).ToString)
                    Exit For
                End If
            Next
            lblName.Text = "Bane"
            lblDept.Text = "Admin"
            lblinout.Text = "In"
            listEmp.Items.Add("Bane Lim")
            txtEmpNum.Clear()

        ElseIf rdTout.Checked = True Then
            lblName.Text = "Bane"
            lblDept.Text = "Admin"
            lblinout.Text = "Out"
            listEmp.Items.Remove("Bane Lim")
            txtEmpNum.Clear()
        End If
Bane Lim
  • 1
  • 2
  • Within the same code block that you are showing the MsgBox, you are also adding "Bane Lim" to the list. Am I missing something? – j.f. Aug 28 '14 at 15:04

1 Answers1

0

Is the problem that the name is appearing a second time? You'll want to Exit Sub or Exit Function rather than Exit For. Exit For is kicking it from the loop but continuing with the remaining code (to add again).

Otherwise add a flag in there like:

    If txtEmpNum.Text = 8888 Then
        If rdTin.Checked = True Then
            Dim bolFound As Boolean = False
            For i As Integer = 0 To listEmp.Items.Count - 1
                If (listEmp.Items(i).ToString.Contains("Bane Lim")) Then
                    MsgBox("String found at " & (i + 1).ToString)
                    bolFound = True
                    Exit For
                End If
            Next
            If Not bolFound Then
                lblName.Text = "Bane"
                lblDept.Text = "Admin"
                lblinout.Text = "In"
                listEmp.Items.Add("Bane Lim")
                txtEmpNum.Clear()
            End If


        ElseIf rdTout.Checked = True Then
            lblName.Text = "Bane"
            lblDept.Text = "Admin"
            lblinout.Text = "Out"
            listEmp.Items.Remove("Bane Lim")
            txtEmpNum.Clear()
        End If
Capellan
  • 717
  • 6
  • 15