📄 form1.frm
字号:
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 5070
ClientLeft = 60
ClientTop = 345
ClientWidth = 7665
LinkTopic = "Form1"
ScaleHeight = 5070
ScaleWidth = 7665
StartUpPosition = 3 'Windows Default
Begin VB.CommandButton cmdList
Caption = "为列表框添加条目:"
Height = 495
Left = 5520
TabIndex = 12
Top = 120
Width = 1935
End
Begin VB.ListBox List1
Height = 3765
Left = 3360
TabIndex = 10
Top = 840
Width = 4095
End
Begin VB.CommandButton cmdRead
Caption = "读取随机文件"
Height = 495
Left = 1800
TabIndex = 9
Top = 4200
Width = 1455
End
Begin VB.CommandButton cmdWrite
Caption = "写入随机文件"
Height = 495
Left = 120
TabIndex = 8
Top = 4200
Width = 1455
End
Begin VB.TextBox txtAge
Height = 495
Left = 1560
TabIndex = 7
Top = 2760
Width = 1455
End
Begin VB.TextBox txtSex
Height = 495
Left = 1560
TabIndex = 5
Top = 2040
Width = 1455
End
Begin VB.TextBox txtName
Height = 495
Left = 1560
TabIndex = 3
Top = 1320
Width = 1455
End
Begin VB.TextBox txtID
Height = 495
Left = 1560
TabIndex = 1
Top = 600
Width = 1455
End
Begin VB.Label Label5
Caption = "人员信息:"
Height = 255
Left = 3960
TabIndex = 11
Top = 360
Width = 1575
End
Begin VB.Label Label4
Caption = "人员年龄:"
Height = 255
Left = 120
TabIndex = 6
Top = 2760
Width = 1215
End
Begin VB.Label Label3
Caption = "人员性别:"
Height = 495
Left = 120
TabIndex = 4
Top = 2040
Width = 1215
End
Begin VB.Label Label2
Caption = "人员姓名:"
Height = 495
Left = 120
TabIndex = 2
Top = 1320
Width = 1215
End
Begin VB.Label Label1
Caption = "人员编号:"
Height = 495
Left = 120
TabIndex = 0
Top = 600
Width = 1215
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private Type Person
PsnID As Integer
PsnName As String * 12
PsnSex As String * 6
PsnAge As Integer
End Type
Private CurRec As Long
Private LastRec As Long
Private TypePsn As Person
Private Sub cmdList_Click()
Dim iFile As Integer
Dim strList As String
Dim iRecLen As Long
'获取自定义类型的长度
iRecLen = Len(TypePsn)
iFile = FreeFile
List1.Clear
Open "C:\RandomTry.gxp" For Random As #iFile Len = iRecLen
If FileLen("C:\RandomTry.gxp") = 0 Then
LastRec = 0
MsgBox "没有记录!"
Exit Sub
Else
LastRec = FileLen("C:\RandomTry.gxp") / iRecLen
End If
CurRec = 1
For CurRec = 1 To LastRec
Get #iFile, CurRec, TypePsn
strList = Str(TypePsn.PsnID) & " " & TypePsn.PsnName & _
" " & TypePsn.PsnSex & " " & Str(TypePsn.PsnAge)
List1.AddItem strList
Next CurRec
'利用Do While循环来遍历文件
'Do While Not EOF(iFile)
'移动文件指针到当前记录,然后读取当前记录
' Seek #iFile, CurRec
' Get #iFile, , TypePsn
'在此处再加一层判断是因为到了记录尾部,Get语句仍然
'会再执行一次,这次将读取出来一些空的记录,然后才能判断
'到了文件的尾部,为了截去这些无用信息,因而需要在此
'加以判断,从而过滤掉多余执行部分取得的数据。
' If Not EOF(iFile) Then
' strList = Str(TypePsn.PsnID) & " " & TypePsn.PsnName & _
' " " & TypePsn.PsnSex & " " & Str(TypePsn.PsnAge)
' List1.AddItem strList
' End If
'存放当前记录位置的变量加1,然后结合Seek语句,实现记录指着的移动
' CurRec = CurRec + 1
'Loop
Close #iFile
End Sub
Private Sub cmdRead_Click()
Dim iFile As Integer
Dim iRecLen As Long
'获取自定义类型的长度
iRecLen = Len(TypePsn)
iFile = FreeFile
Open "C:\RandomTry.gxp" For Random As #iFile Len = iRecLen
If FileLen("C:\RandomTry.gxp") = 0 Then
LastRec = 0
MsgBox "没有记录!"
Exit Sub
Else
'这个技巧用来获取随机文件中的记录数量
'如果自定义类型不是定长的字符串,那么Len函数可能
'不能返回正确的自定义类型的大小,那么该方法可能就
'不能获取正确的结果。
LastRec = FileLen("C:\RandomTry.gxp") / iRecLen
End If
'用来移动到第二条记录
'然后Get或Put函数可以不指定记录位置,直接访问
'通过Seek移动到的位置上的记录
Seek #iFile, 2
Get #iFile, , TypePsn
txtID.Text = TypePsn.PsnID
txtName.Text = TypePsn.PsnName
txtSex.Text = TypePsn.PsnSex
txtAge.Text = TypePsn.PsnAge
Close #iFile
End Sub
Private Sub cmdWrite_Click()
Dim iFile As Integer
Dim iRecLen As Long
'获取自定义类型的长度
iRecLen = Len(TypePsn)
iFile = FreeFile
On Error GoTo err
TypePsn.PsnID = CInt(Trim$(txtID.Text))
TypePsn.PsnName = Trim$(txtName.Text)
TypePsn.PsnSex = Trim$(txtSex.Text)
TypePsn.PsnAge = CInt(Trim$(txtAge.Text))
'如果不存在该文件,那么Open语句将自动创建一个文件
Open "C:\RandomTry.gxp" For Random As #iFile Len = iRecLen
If FileLen("C:\RandomTry.gxp") = 0 Then
LastRec = 0
Else
LastRec = FileLen("C:\RandomTry.gxp") / iRecLen
End If
LastRec = LastRec + 1
Put #iFile, LastRec, TypePsn
Close #iFile
Exit Sub
err:
MsgBox err.Number & " " & err.Description
End Sub
Private Sub Form_Load()
List1.Clear
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -