实验8-7
在教学篇例8.7的基础上增加“修改确定”按钮以及用于定位记录的按钮面板,如图2.8.5所示,并编写时间过程。
解题,代码如下:
先建立标准模块,代码是:
Type studtype iNo As Integer strName As String * 20 strSex As String * 1 sMark As Single End Type
再在窗体上建立相应的按钮等控件,窗体代码如下:
Dim Student As studtype Dim Record_No As Integer Sub Form_Load() Open “C:\STUDENT.DAT” For Random As #1 Len = Len(Student) Label1.Caption = LOF(1) / Len(Student) Close #1 If Label1.Caption <> “0” Then Command3_Click ‘记录不为零,就显示第一条记录 End Sub
Sub Command1_Click() ‘追加记录 With Student .iNo = Val(Text1.Text) .strName = Text2.Text .strSex = IIf(Option1.Value, “1”, “0”) .sMark = Val(Text3.Text) End With
Open “C:\STUDENT.DAT” For Random As #1 Len = Len(Student) Record_No = LOF(1) / Len(Student) + 1 Label1.Caption = Record_No Text4.Text = Record_No ‘加这句,当场修改记录就不用手工输入了 Put #1, Record_No, Student Close #1 End Sub
Sub Command2_Click() ‘显示记录 Open “C:\STUDENT.DAT” For Random As #1 Len = Len(Student) Record_No = Val(Text4.Text) Get #1, Record_No, Student Text1.Text = Student.iNo Text2.Text = Student.strName If Student.strSex = “1” Then Option1.Value = True Else Option2.Value = True End If Text3.Text = Student.sMark Record_No = LOF(1) / Len(Student) Close #1 End Sub
Private Sub Command3_Click() ‘第一条记录 Open “C:\STUDENT.DAT” For Random As #1 Len = Len(Student) Record_No = 1 Get #1, Record_No, Student Text1.Text = Student.iNo Text2.Text = Student.strName If Student.strSex = “1” Then Option1.Value = True Else Option2.Value = True End If Text3.Text = Student.sMark Text4.Text = “1” Close #1
End Sub Private Sub Command4_Click() ‘下一条记录 If Record_No = Val(Label1.Caption) Then MsgBox “已经是最后一条记录了” Exit Sub ‘已经是最后一条记录就退出。 End If
Open “C:\STUDENT.DAT” For Random As #1 Len = Len(Student) Record_No = Record_No + 1 Get #1, Record_No, Student Text1.Text = Student.iNo Text2.Text = Student.strName If Student.strSex = “1” Then Option1.Value = True Else Option2.Value = True End If Text3.Text = Student.sMark Text4.Text = Record_No Close #1 End Sub
Private Sub Command5_Click() ‘前一条记录 If Record_No = 1 Then MsgBox “已经是第一条记录了” Exit Sub ‘已经是第一条记录就退出。 End If
Open “C:\STUDENT.DAT” For Random As #1 Len = Len(Student) Record_No = Record_No – 1 Get #1, Record_No, Student Text1.Text = Student.iNo Text2.Text = Student.strName If Student.strSex = “1” Then Option1.Value = True Else Option2.Value = True End If Text3.Text = Student.sMark Text4.Text = Record_No Close #1 End Sub
Private Sub Command6_Click() ‘最后一条记录 Open “C:\STUDENT.DAT” For Random As #1 Len = Len(Student) Record_No = Val(Label1.Caption) Get #1, Record_No, Student Text1.Text = Student.iNo Text2.Text = Student.strName If Student.strSex = “1” Then Option1.Value = True Else Option2.Value = True End If Text3.Text = Student.sMark Text4.Text = Label1.Caption Close #1 End Sub
Sub Command7_Click() ‘修改记录 With Student .iNo = Val(Text1.Text) .strName = Text2.Text .strSex = IIf(Option1.Value, “1”, “0”) .sMark = Val(Text3.Text) End With
Open “C:\STUDENT.DAT” For Random As #1 Len = Len(Student) Record_No = Val(Text4.Text) Put #1, Record_No, Student Close #1 End Sub
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/185047.html原文链接:https://javaforall.cn