以下是我写的抽奖系统中的生成中奖人员名单的文本功能。但是出现的情况时,当存在“中奖人员名单.txt”时,生成的文本数据不更新。必须将程序退出后再生成,才会更新。烦请帮忙检查是什么原因。谢谢!
Private Sub mnuTxt_Click()Dim i As IntegerIf Dir(App.Path & "\中奖人员名单.txt") <> "" Then
Kill App.Path & "\中奖人员名单.txt" For i = 0 To FrmSelect.List1.ListCount - 1 Open App.Path & "\中奖人员名单.txt" For Append As #1
Print #1, FrmSelect.List1.List(i) 'FrmSelect.List1是显示中奖人员名单
Close #1
Next
Else For i = 0 To FrmSelect.List1.ListCount - 1
Open App.Path & "\中奖人员名单.txt" For Append As #1
Print #1, FrmSelect.List1.List(i)
Close #1
Next
End If
End Sub
Private Sub mnuTxt_Click()Dim i As IntegerIf Dir(App.Path & "\中奖人员名单.txt") <> "" Then
Kill App.Path & "\中奖人员名单.txt" For i = 0 To FrmSelect.List1.ListCount - 1 Open App.Path & "\中奖人员名单.txt" For Append As #1
Print #1, FrmSelect.List1.List(i) 'FrmSelect.List1是显示中奖人员名单
Close #1
Next
Else For i = 0 To FrmSelect.List1.ListCount - 1
Open App.Path & "\中奖人员名单.txt" For Append As #1
Print #1, FrmSelect.List1.List(i)
Close #1
Next
End If
End Sub
Dim nFreeNum As LongnFreeNum = FreeFileIf Dir(App.Path & "\中奖人员名单.txt") <> "" Then
Kill App.Path & "\中奖人员名单.txt" For i = 0 To List1.ListCount - 1 Open App.Path & "\中奖人员名单.txt" For Append As #nFreeNum
Print #nFreeNum, List1.List(i) 'FrmSelect.List1是显示中奖人员名单
Close #nFreeNum
Next
Else For i = 0 To List1.ListCount - 1
Open App.Path & "\中奖人员名单.txt" For Append As #nFreeNum
Print #nFreeNum, List1.List(i)
Close #nFreeNum
Next
End If
Option ExplicitPrivate Sub Command1_Click()
Dim i As Integer
Dim intFileNum As Integer
If Dir(App.Path & "\中奖人员名单.txt") <> "" Then
Kill App.Path & "\中奖人员名单.txt"
For i = 0 To FrmSelect.List1.ListCount - 1
intFileNum = FreeFile
Open App.Path & "\中奖人员名单.txt" For Append As #intFileNum
Print #intFileNum, FrmSelect.List1.List(i) 'FrmSelect.List1是显示中奖人员名单
Close #intFileNum
Next
Else
For i = 0 To FrmSelect.List1.ListCount - 1
intFileNum = FreeFile
Open App.Path & "\中奖人员名单.txt" For Append As #intFileNum
Print #intFileNum, FrmSelect.List1.List(i)
Close #intFileNum
Next
End IfEnd Sub
第二次启动时把以前的Kill掉,然后再新建一个
Dim i As Integer
For i = 0 To FrmSelect.List1.ListCount - 1
Open App.Path & "\中奖人员名单.txt" For Output As #1
Print #1, FrmSelect.List1.List(i) 'FrmSelect.List1是显示中奖人员名单
Close #1
Next
End Sub
Private Sub mnuTxt_Click()
Dim i As Integer
Open App.Path & "\中奖人员名单.txt" For Output As #1
For i = 0 To FrmSelect.List1.ListCount - 1
Print #1, FrmSelect.List1.List(i) 'FrmSelect.List1是显示中奖人员名单
Next
Close #1
End Sub
我现在的问题是,在程序运行中,只能有效的执行一次生成中奖人员名单.txt ,程序未退出时,进行第二次或第二次以上的上述操作的话,中奖人员名单.txt中的记录仍是第一次生成的。
如果存在“中奖人员名单.txt”时,程序是能将它kill,重新生成的。但是记录不能随FrmSelect窗体中的list1的记录进行更新。
DoEvents或者将mnuTxt_Click中的代码放Timer计时器中执行。似乎单次按钮事件中代码未执行完,VB程序它处有变动是不会执行的。
但是第一次执行mnuTxt_Click是肯定没问题的哦。数据是更新过来的。