form1.vb
来自「Visual Basic .NET程序设计教程源代码」· VB 代码 · 共 251 行
VB
251 行
Public Class Form1
Inherits System.Windows.Forms.Form
Structure RecordType
<VBFixedString(10)> Dim EmName As String
<VBFixedString(16)> Dim Wunit As String
Dim Age As Short
Dim Salary As Short
End Structure
Dim recordvar As RecordType
Dim recLen As Integer = Len(recordvar)
Dim position As Integer
Dim recordnumber As Long
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Button3 As System.Windows.Forms.Button
Friend WithEvents Button4 As System.Windows.Forms.Button
Friend WithEvents Button5 As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
Friend WithEvents TextBox4 As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.Button3 = New System.Windows.Forms.Button
Me.Button4 = New System.Windows.Forms.Button
Me.Button5 = New System.Windows.Forms.Button
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.Label3 = New System.Windows.Forms.Label
Me.Label4 = New System.Windows.Forms.Label
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.TextBox3 = New System.Windows.Forms.TextBox
Me.TextBox4 = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(48, 104)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(104, 23)
Me.Button1.TabIndex = 0
Me.Button1.Text = "显示前一个记录"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(208, 104)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(104, 23)
Me.Button2.TabIndex = 1
Me.Button2.Text = "显示下一个记录"
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(16, 144)
Me.Button3.Name = "Button3"
Me.Button3.TabIndex = 2
Me.Button3.Text = "打开文件"
'
'Button4
'
Me.Button4.Location = New System.Drawing.Point(144, 144)
Me.Button4.Name = "Button4"
Me.Button4.TabIndex = 3
Me.Button4.Text = "修改数据"
'
'Button5
'
Me.Button5.Location = New System.Drawing.Point(280, 144)
Me.Button5.Name = "Button5"
Me.Button5.TabIndex = 4
Me.Button5.Text = "退出程序"
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(24, 24)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(64, 23)
Me.Label1.TabIndex = 5
Me.Label1.Text = "姓名"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(112, 24)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(56, 23)
Me.Label2.TabIndex = 6
Me.Label2.Text = "单位"
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(200, 24)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(64, 23)
Me.Label3.TabIndex = 7
Me.Label3.Text = "年龄"
'
'Label4
'
Me.Label4.Location = New System.Drawing.Point(296, 24)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(72, 23)
Me.Label4.TabIndex = 8
Me.Label4.Text = "工资"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(16, 56)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(72, 21)
Me.TextBox1.TabIndex = 9
Me.TextBox1.Text = ""
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(104, 56)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(72, 21)
Me.TextBox2.TabIndex = 10
Me.TextBox2.Text = ""
'
'TextBox3
'
Me.TextBox3.Location = New System.Drawing.Point(192, 56)
Me.TextBox3.Name = "TextBox3"
Me.TextBox3.Size = New System.Drawing.Size(80, 21)
Me.TextBox3.TabIndex = 11
Me.TextBox3.Text = ""
'
'TextBox4
'
Me.TextBox4.Location = New System.Drawing.Point(288, 56)
Me.TextBox4.Name = "TextBox4"
Me.TextBox4.Size = New System.Drawing.Size(80, 21)
Me.TextBox4.TabIndex = 12
Me.TextBox4.Text = ""
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(384, 189)
Me.Controls.Add(Me.TextBox4)
Me.Controls.Add(Me.TextBox3)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Label4)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.Button5)
Me.Controls.Add(Me.Button4)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "通过界面显示和修改随"
Me.ResumeLayout(False)
End Sub
#End Region
Sub display()
FileGet(1, recordvar, position)
TextBox1.Text = recordvar.EmName
TextBox2.Text = recordvar.Wunit
TextBox3.Text = recordvar.Age
TextBox4.Text = recordvar.Salary
End Sub
Private Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
FileOpen(1, "d:\temp\employee.txt", OpenMode.Random, OpenAccess.ReadWrite, , recLen)
recordnumber = LOF(1) / Len(recordvar)
position = 1
display()
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
If position > 1 Then
position -= 1
display()
ElseIf position = 1 Then
MsgBox("这是第一个记录", , "")
End If
End Sub
Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim msg As String
Dim resp As Short
If position < recordnumber Then
position = position + 1
display()
ElseIf position = recordnumber Then
msg = "这是最后一个记录" + Chr(13) + Chr(10)
msg = msg + "是否关闭文件?"
resp = MsgBox(msg, 36, "请选择")
If resp = 6 Then
FileClose(1)
End
End If
End If
End Sub
Private Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
recordvar.EmName = TextBox1.Text
recordvar.Wunit = TextBox2.Text
recordvar.Age = TextBox3.Text
recordvar.Salary = TextBox4.Text
FilePut(1, recordvar, position)
End Sub
Private Sub Button5_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button5.Click
End
End Sub
End Class
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?