保存代碼如下:
Private Sub btnSAVE_Click(sender As Object, e As EventArgs) Handles btnSAVE.Click
Dim Person As AdressList '結構體變數,用于保存檔案中的一條記錄
Dim RecCount As Integer '檔案中的記錄總數
Dim I As Integer
If Trim(txtNUM.Text) = "" Then '編號為零,退出本程序
MsgBox("編號不能為空")
txtNUM.Focus()
Exit Sub
End If
txtNUM.Text = Convert.ToString(Val(txtNUM.Text)) '清除文本框中的非數字字符
txtNUM.Text = Mid(txtNUM.Text + " ", 1, 4) '在編號后面補上空格
'回傳首記錄
If LOF(1) > 0 Then
FileGet(1, Person, 1)
End If
'判斷新的編號在記錄中是否存在
RecCount = 1
Do While EOF(1) = False
FileGet(1, Person, RecCount)
If Person.NUM = txtNUM.Text And State = "新記錄" Then
MsgBox("相同的編號已經存在,請從新設定一個編號")
txtNUM.Text = ""
txtNUM.Focus()
Exit Sub
End If
RecCount = RecCount + 1
Loop
txtNAME.Text = Trim(txtNAME.Text)
If txtNAME.Text.Length = 0 Then
MsgBox("名字不能為空,請注意")
txtNAME.Focus()
Exit Sub
End If
Person.NUM = txtNUM.Text
Person.NAME = txtNAME.Text
Person.SEX = rbdMAN.Checked
Person.CITY = txtCITY.Text
Person.ADRESS = txtADRESS.Text
Person.PHONE = txtPHONE.Text
Person.CELL = txtCELL.Text
'準備寫入檔案
If State = "新記錄" Then
RecCount = LastRec() + 1 '當前記錄數 運行到此處報錯,內容為{"錯誤的記錄長度。"}
FilePut(1, Person, RecCount) '將新編碼,姓名等增加到串列框中
istNAME.Items.Add(txtNUM.Text + "" + txtNAME.Text)
End If
If State = "編輯" Then
FilePut(1, Person, BookMark)
State = "新記錄"
'改寫串列框的內容
istNAME.Items.Clear()
RecCount = LastRec()
For I = 1 To RecCount
FileGet(1, Person, I)
istNAME.Items.Add(Person.NUM + "" + Person.NAME)
Next
End If
txtNUM.Text = ""
txtNAME.Text = ""
txtCITY.Text = ""
txtADRESS.Text = ""
txtPHONE.Text = ""
txtCELL.Text = ""
rbdMAN.Checked = True
txtNUM.Focus()
End Sub
通用函式程序 LastRec() 如下
Private Function LastRec() As Integer
Dim RecCount As Integer
'檔案中的總記錄數
Dim Person As AdressList
'結構體變數用于保存檔案中的一條記錄
If LOF(1) = 0 Then
'如果檔案長度為0,則記錄數為0
LastRec = 0
Exit Function
End If
RecCount = 1
FileGet(1, Person, RecCount)
Do While EOF(1) = False
RecCount = RecCount + 1
FileGet(1, Person, RecCount)
Loop
MsgBox("當前記錄數為:" & istNAME.Items.Count & "" & RecCount & "條")
LastRec = RecCount
End Function
uj5u.com熱心網友回復:
是自學還是老師在教?你這學的是用vb.net的皮去套vb6的寫法,可別誤入歧途。轉載請註明出處,本文鏈接:https://www.uj5u.com/net/123705.html
標籤:VB.NET
下一篇:不知道是否需要添加WEB應用
