回完一个,发现竟然回错了贴。
按这个写的,没按那个写。
==================================
问题是: 最近没事,已顺手写了。
论坛里用文件操作的 感觉比较少。
所有的数据都是使用的文本类型,是为了跟TXT文件兼容而故意的。
其实应该把某个字段定义为 long 类型。
把以下内容复制到 记事本里,然后保存为 后缀为 FRM 的文件。
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 1995
ClientLeft = 120
ClientTop = 450
ClientWidth = 6135
LinkTopic = "Form1"
ScaleHeight = 1995
ScaleWidth = 6135
StartUpPosition = 3 '窗口缺省
Begin Command9
Caption = "删除"
Height = 375
Left = 4800
TabIndex = 23
Top = 1440
Width = 495
End
Begin Command8
Caption = "关闭"
Height = 375
Left = 5400
TabIndex = 22
Top = 1440
Width = 615
End
Begin Command7
Caption = "还原"
Height = 375
Left = 4080
TabIndex = 21
Top = 1440
Width = 615
End
Begin Command6
Caption = "新增"
Height = 375
Left = 3360
TabIndex = 20
Top = 1440
Width = 615
End
Begin Command5
Caption = "保存"
Height = 375
Left = 2640
TabIndex = 19
Top = 1440
Width = 615
End
Begin Command4
Caption = ">|"
Height = 375
Left = 2160
TabIndex = 18
Top = 1440
Width = 375
End
Begin Command3
Caption = ">"
Height = 375
Left = 1800
TabIndex = 17
Top = 1440
Width = 375
End
Begin Command2
Caption = "<"
Height = 375
Left = 600
TabIndex = 15
Top = 1440
Width = 375
End
Begin Command1
Caption = "|<"
Height = 375
Left = 240
TabIndex = 14
Top = 1440
Width = 375
End
Begin Combo1
Height = 300
Left = 4920
Style = 2 'Dropdown List
TabIndex = 13
Top = 240
Width = 1095
End
Begin VB.TextBox Text7
Height = 270
Left = 840
TabIndex = 12
Top = 960
Width = 5175
End
Begin VB.TextBox Text6
Height = 270
Left = 4920
TabIndex = 10
Top = 600
Width = 1095
End
Begin VB.TextBox Text5
Height = 270
Left = 2880
TabIndex = 8
Top = 600
Width = 1095
End
Begin VB.TextBox Text4
Height = 270
Left = 840
TabIndex = 6
Top = 600
Width = 1095
End
Begin VB.TextBox Text2
Height = 270
Left = 2880
TabIndex = 3
Top = 240
Width = 1095
End
Begin VB.TextBox Text1
Height = 270
Left = 840
TabIndex = 1
Top = 240
Width = 1095
End
Begin VB.Label Label8
Alignment = 2 'Center
Appearance = 0 'Flat
BackColor = &H80000005&
BorderStyle = 1 'Fixed Single
Caption = "Label8"
ForeColor = &H80000008&
Height = 375
Left = 960
TabIndex = 16
Top = 1440
Width = 855
End
Begin VB.Label Label7
Caption = "籍贯:"
Height = 255
Left = 120
TabIndex = 11
Top = 960
Width = 735
End
Begin VB.Label Label6
Caption = "班级:"
Height = 255
Left = 4320
TabIndex = 9
Top = 600
Width = 735
End
Begin VB.Label Label5
Caption = "院系:"
Height = 255
Left = 2280
TabIndex = 7
Top = 600
Width = 735
End
Begin VB.Label Label4
Caption = "年龄:"
Height = 255
Left = 120
TabIndex = 5
Top = 600
Width = 735
End
Begin VB.Label Label3
Caption = "性别:"
Height = 255
Left = 4320
TabIndex = 4
Top = 240
Width = 735
End
Begin VB.Label Label2
Caption = "姓名:"
Height = 255
Left = 2280
TabIndex = 2
Top = 240
Width = 735
End
Begin VB.Label Label1
Caption = "学号:"
Height = 255
Left = 120
TabIndex = 0
Top = 240
Width = 735
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 学生信息类型
学号 As String * 6
姓名 As String * 5
性别 As String * 1
年龄 As String * 3
院系 As String * 8
班级 As String * 8
籍贯 As String * 60
分隔符 As String * 2
End Type
Private Const DataFile = "Data.txt"
Private 记录号 As Long
Private 记录数 As Long
Private 性别数组(1 To 3) As String
Private Sub Command1_Click()
Call view(1)
End Sub
Private Sub Command2_Click()
Call view(记录号 - 1)
End Sub
Private Sub Command3_Click()
Call view(记录号 + 1)
End Sub
Private Sub Command4_Click()
Call view(9999999)
End Sub
Private Sub Command5_Click()
Call save(记录号)
End Sub
Private Sub Command6_Click()
Call save(0)
End Sub
Private Sub Command7_Click()
Call view(记录号)
End Sub
Private Sub Command8_Click()
Unload Me
End Sub
Private Sub Command9_Click()
Call del(记录号)
End Sub
Private Sub Form_Load()
Dim i As Long
性别数组(1) = "男"
性别数组(2) = "女"
性别数组(3) = " "
For i = 1 To 3
Combo1.AddItem 性别数组(i)
Next i
Dim fr As Long
Dim o As 学生信息类型
fr = FreeFile
Open DataFile For Random As #fr Len = Len(o)
i = LOF(fr) / Len(o)
记录数 = i
记录号 = 1
Label8.Caption = 记录号 & " / " & 记录数
Close #fr
Call view(1)
End Sub
Public Function 加空格(cs1 As String, cs2 As Long) As String
'在尾加空格
Dim tmpb() As Byte, m As Long
tmpb = StrConv(cs1, vbFromUnicode)
m = cs2 - UBound(tmpb)
If m > 0 Then
加空格 = cs1 & Space(m)
Else
加空格 = cs1
End If
End Function
Public Sub view(cs As Long)
Dim fr As Long
Dim o As 学生信息类型
fr = FreeFile
Dim i As Long
If cs < 1 Then cs = 1
Open DataFile For Random As #fr Len = Len(o)
i = LOF(fr) / Len(o)
If cs > i Then cs = i
Get #fr, cs, o
记录号 = cs
Close #fr
Text1.Text = o.学号
Text2.Text = o.姓名
Combo1.ListIndex = Val(o.性别) - 1
Text4.Text = o.年龄
Text5.Text = o.院系
Text6.Text = o.班级
Text7.Text = o.籍贯
Label8.Caption = 记录号 & " / " & 记录数
End Sub
Public Sub save(Optional cs As Long = 0)
Dim fr As Long
Dim o As 学生信息类型
fr = FreeFile
Dim i As Long
o.学号 = Text1.Text
o.姓名 = Text2.Text
o.性别 = Combo1.ListIndex + 1
o.年龄 = Text4.Text
o.院系 = Text5.Text
o.班级 = Text6.Text
o.籍贯 = Text7.Text
o.分隔符 = vbCrLf
Open DataFile For Random As #fr Len = Len(o)
i = LOF(fr) / Len(o)
If cs = 0 Then
cs = i + 1
记录号 = cs
记录数 = cs
End If
If cs > i Then
cs = i + 1
记录号 = cs
记录数 = cs
End If
Put #fr, cs, o
Close #fr
Label8.Caption = 记录号 & " / " & 记录数
End Sub
Public Sub del(cs As Long)
Dim fr As Long, fr2 As Long
Dim o As 学生信息类型
Dim i As Long, j As Long
fr = FreeFile
Open DataFile For Random As #fr Len = Len(o)
i = LOF(fr) / Len(o)
fr2 = FreeFile
Open "tmp.tmp" For Random As #fr2 Len = Len(o)
For j = 1 To i
Get #fr, j, o
If j <> cs Then
Put #fr2, , o
End If
Next j
Close #fr2
Close #fr
Kill DataFile
Name "tmp.tmp" As DataFile
记录数 = i - 1
Call view(记录号)
End Sub