Search code examples
vb.netlistboxtime-and-attendance

VB.NET LISTBOX [in and out]


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

Solution

  • 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