当前位置:首页 » VB程序题 » 正文

VB程序题:在教材例8.6中添加一组按钮,如图H.5所示,并编写事件过程。龚沛曾

VB程序题:在教材例8.6中添加一组按钮,如图H.5所示,并编写事件过程。龚沛曾

图H.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

Visual Basic程序设计教程(第3版) (龚沛曾等编)课后实验源码

打赏 支付宝打赏 微信打赏

来源:济亨网

本文链接:https://wb98.com/post/111.html

    << 上一篇 下一篇 >>

    湘公网安备 43011102000514号 - 湘ICP备08100508号