【发布时间】:2016-03-14 22:59:34
【问题描述】:
我正在开发一个非常简单的工作时钟应用程序。我有一个填充了员工姓名的组合框和一个显示哪些员工已登录的列表框。如果员工已经登录(显示在 ListIn 列表框中),则子应通知用户并退出而不尝试添加该人再次添加到 ListIn 框中。不幸的是,我收到“已经打卡”消息,但此人再次添加到 ListIn 框中。
这是我的代码:
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
对于这个小问题的任何帮助将不胜感激。我正在使用 VB.NET 2010 Professional,如果这很重要的话。
谢谢大家。
【问题讨论】:
-
你试过调试了吗?
-
a) 打开 Option Strict b) 您正在添加到循环中的列表中,以便在有人打卡后,您还应该得到已经打卡的 msg c) 如果控件使用 DataSource 你可能很容易过滤。