下面是我做的一个简单程序,请各位高手帮我看看我该怎么办。在窗体上添加三个命令按钮,名称及Caption为CmdNewFile,"新建";CmdOpenFile,"打开";CmdAddRecord,"添加"。添加两个CommonDialog控件,名称为Dlg1,Dlg2,添加一个文本框控件Text1,一个组合框控件Combo1。 我的程序是运行后单击CmdNewFile新建(或单击CmdOpenFile打开)一磁盘随机文件,然后可通过单击CmdAddRecord按钮向文件添加记录,并将记录的.part元素与组合框的.Text比较,如果不相同则向组合框添加项目,如果相同则不添加。我的问题是程序运行新建一随机文件后,运行中添加记录,组合框添加项目一切正常,但结束程序后重新运行程序,用单击CmdOpenFile打开刚才创建的文件,组合框添中项目时不论相同与否却全部重复添加,即记录的.part元素与组合框的.Text比较结果都是False。我无法理解,更不明白这是为什么,请各位帮我分析一下。不胜感激! Option Explicit Private Type brakeAtt Name As String * 20 Part As String * 10 End Type
Dim Sbrake As brakeAtt '自定义记录类型变量 Dim Brakefilenum As Integer '文件号 Dim SrecordLen As Long '记录长度 Dim CurrentBrake As Long '当前记录号的变量 Dim LastBrake As Long '最后记录号的变量 Dim PresentfileName As String '现存文件名及路径
Private Sub SaveCurrentBrake() With Sbrake .Name = Text1.Text .Part = Combo1.Text End With '将变量sbrake的内容写入当前记录 Put #Brakefilenum, CurrentBrake, Sbrake End Sub
Private Sub OpenFile() Brakefilenum = FreeFile SrecordLen = Len(Sbrake) Open PresentfileName For Random As Brakefilenum Len = SrecordLen CurrentBrake = 1 LastBrake = LOF(Brakefilenum) / SrecordLen End Sub
Private Sub Showcurrentbrake() Get #Brakefilenum, CurrentBrake, Sbrake '将变量sbrake的值赋给控件显示 Text1.Text = Trim(Sbrake.Name) Combo1.Text = Trim(Sbrake.Part) End Sub
Private Sub ShowCurrentPart() '向组合框添加项目 Dim i As Integer Dim j As Integer For i = 1 To LastBrake Get #Brakefilenum, i, Sbrake For j = 0 To Combo1.ListCount - 1 If Trim(Sbrake.Part) = Trim(Combo1.List(j)) Then Exit For '比较 Next j If j = Combo1.ListCount Then Combo1.AddItem Trim(Sbrake.Part) '添加项目 End If Next i End Sub
Private Sub CmdAddRecord_Click() '文件添加记录 Dim i As Integer Dim k As Integer k = LOF(Brakefilenum) / SrecordLen For i = 1 To k Get Brakefilenum, i, Sbrake If Trim(Sbrake.Name) = Trim(Text1.Text) And Trim(Sbrake.Part) = Trim(Combo1.Text) Then Exit For Next i If i = k + 1 Then LastBrake = LastBrake + 1 '增加一条空白记录 CurrentBrake = LastBrake SaveCurrentBrake '保存当前记录 ElseIf i < k + 1 Then MsgBox "不允许存在同名且位置相同的记录,请重新添加。", vbCritical, "提醍您!" Exit Sub End If
ShowCurrentPart '将输入窗口置空 Text1.Text = "" Combo1.Text = "" Text1.SetFocus End Sub
Private Sub CmdNewFile_Click() On Error GoTo err Dim ans As Integer ans = 0 If Me.Caption <> "记录管理" Then ans = MsgBox("您确定要保存并退出" & PresentfileName & "吗?", vbOKCancel, "提醍您!") If ans = 2 Then Exit Sub ElseIf ans = 1 Then Combo1.Clear Close Me.Caption = "记录管理" End If Dlg2.FileName = "NewRecord" Dlg2.DialogTitle = "选择您要保存的位置" Dlg2.Filter = "记录文件*.hgq|*.hgq" Dlg2.Flags = cdlOFNOverwritePrompt '同名时询问是否替换 Dlg2.ShowSave
PresentfileName = Dlg2.FileName Call OpenFile Me.Caption = Me.Caption & PresentfileName err:
End Sub
Private Sub CmdOpenFile_Click() On Error GoTo err Dim ans As Integer Dim i As Integer, j As Integer ans = 0 If Me.Caption <> "记录管理" Then ans = MsgBox("您确定要保存并退出" & PresentfileName & "吗?", vbOKCancel, "提醍您!") If ans = 2 Then Exit Sub ElseIf ans = 1 Then Combo1.Clear Close Me.Caption = "记录管理" End If
Dlg1.FileName = "" Dlg1.Filter = "记录文件(*.hgq)|*.hgq" Dlg1.DialogTitle = "打开记录文件(.hgq)" Dlg1.Flags = 4096 Dlg1.ShowOpen PresentfileName = Dlg1.FileName
Call OpenFile Me.Caption = Me.Caption & PresentfileName
Showcurrentbrake ShowCurrentPart err: End Sub
Private Sub Form_Load() Me.Caption = "记录管理" End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) Text1.Enabled = IIf(Me.Caption = "记录管理", False, True) Combo1.Enabled = Text1.Enabled CmdAddRecord.Enabled = IIf(Text1.Text = "" Or Combo1.Text = "", False, True) End Sub
Private Sub Form_Unload(Cancel As Integer) Close End Sub