vb求助——
本人小白,初学vb,求大虾指点,谢谢以下是某年某月某日某时提示某事件的源代码
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
listwork.Items.Remove(listwork.SelectedItem)
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim i As Integer
Dim s As Date
Dim s1 As String
Dim s2 As String
Dim s4 As String
Dim s5 As String
Dim p As Integer
Dim j As Integer
Dim m As Integer
Dim s3 As String
Dim s6 As String
s1 = Date.Now.ToShortDateString
s4 = Date.Now.ToShortTimeString
p = Len(s1)
m = Len(s4) + p
For Each s2 In listwork.Items()
s3 = s2.Substring(i, p)
If s1 = s3 Then
For Each s5 In listwork.Items()
s6 = s5.Substring(p + 1, m)
If s4 = s6 Then
MsgBox(s2)
End If
Next
End If
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
listwork.Items.Add(txtday.Text & txttime.Text & txtwork.Text)
End Sub
End Class --------------------编程问答-------------------- 转到.NET区吧 --------------------编程问答-------------------- lz的结贴率0.00%啊!
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
listwork.Items.Remove(listwork.SelectedItem) 'listbox移除选择的项
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim i As Integer
Dim s As Date
Dim s1 As String
Dim s2 As String
Dim s4 As String
Dim s5 As String
Dim p As Integer
Dim j As Integer
Dim m As Integer
Dim s3 As String
Dim s6 As String
s1 = Date.Now.ToShortDateString '当前日期,例如2011-12-13
s4 = Date.Now.ToShortTimeString '当前时间,例如10:25
p = Len(s1) '日期字符串的长度
m = Len(s4) + p '日期+时间字符串的长度
For Each s2 In listwork.Items() '遍历listbox,看来listbox都是类似"2011-12-13 10:25"这种格式
s3 = s2.Substring(i, p) 'S3取每一条list的前p个字符,也就是"2011-12-13"
If s1 = s3 Then '如果是今天日期
For Each s5 In listwork.Items() '然后再次遍历listbox,
s6 = s5.Substring(p + 1, m) '取p+1开始的m个长度,即时间"10:25"
If s4 = s6 Then '如果是当前的时间
MsgBox(s2) '提示listbox中这一条
End If
Next
End If
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
listwork.Items.Add(txtday.Text & txttime.Text & txtwork.Text) '添加list
End Sub
End Class
不过,上面判断的有点问题啊,进行了两次循环这导致一个严重的比对bug
例如当前时间"2011-12-13 10:35"
列表里面有
1 "2011-12-12 10:12"
2 "2011-12-12 10:35"
3 "2011-12-13 10:12"
4 "2011-12-13 10:35"
外层循环首先会先找到3的记录,然后进行内层循环找到2的记录,提示3,再找到4的记录,提示3
然后回到外层循环找到4的记录,然后进行内层循环找到2的记录,提示4,再找到4的记录,提示4
这明显是错误的..
应该只提示4才对吧.
--------------------编程问答-------------------- 时间类型就用时间类型,用个字符串拆来拆去,弄的算法也不清晰,错误也多。
补充:.NET技术 , VB.NET