我在学习VB“访问随即文件”时遇到一个程序搞不明白,请大家帮帮我。
要求就是利用随即文件保存学生成绩,可以输入学生的学号、姓名以及三门功课的成绩,浏览或删除数据。我向大家请教的问题是,为什么程序运行时不能达到Command2.Enabled = False和Command3.Enabled = False的效果。
代码如下:
Private Type cj
xm As String * 6
xh As String * 6
sx As Integer
yw As Integer
wy As Integer
End Type
Private da As cj
Private Sub Command1_Click()
For i = 0 To 4
Text1(i).Text = ""
Next
Text1(0).SetFocus
End Sub
Private Sub Command2_Click()
Dim lastrec As Integer
With da
.xh = Text1(0).Text
.xm = Text1(1).Text
.yw = Val(Text1(2).Text)
.wy = Val(Text1(3).Text)
.sx = Val(Text1(4).Text)
End With
Open "xsda2.dat" For Random As #1 Len = Len(da)
lastrec = LOF(1) / Len(da)
Put #1, lastrec + 1, da
Close #1
Call Form_Load
Text1(0).SetFocus
End Sub
Private Sub Command3_Click()
Dim lastrec As Integer
recnum = List1.ListIndex + 1
Open "rec.tem" For Random As #1 Len = Len(da)
Open "xsda2.dat" For Random As #2 Len = Len(da)
lastrec = LOF(2) / Len(da)
For n = 1 To lastrec
If n <> recnum Then
Get #2, n, da
Put #1, , da
Else
Get #2, n, da
With da
Text1(0).Text = .xh
Text1(1).Text = .xm
Text1(2).Text = .yw
Text1(3).Text = .wy
Text1(4).Text = .sx
End With
End If
Next
Close #1
Close #2
Kill "xsda2.dat"
Name "rec.tem" As "xsda2.dat"
Call Form_Load
Text1(0).SetFocus
End Sub
Private Sub Form_Load()
Dim lastrec As Integer
Open "xsda2.dat" For Random As #1 Len = Len(da)
lastrec = LOF(1) / Len(da)
List1.Clear
For n = 1 To lastrec
Get #1, n, da
With da
xh = Format(.xh, "@@@@@@")
xm = Format(RTrim(.xm), "@@@@")
yw = Format(.yw, "####")
sx = Format(.sx, "####")
msg = xh & xm & "" & yw & "" & wy & "" & sx
End With
List1.AddItem msg
Next
Close #1
If List1.ListIndex > -1 Then
Command2.Enabled = False
Else
Command2.Enabled = True
End If
End Sub
Private Sub List1_Click()
If List1.ListIndex > -1 Then
Command3.Enabled = True
Else
Command3.Enabled = False
End If
End Sub