这段代码在VB6中运行时,老是有错误提示
请帮忙给改一下,只要能正常运行就可以!谢谢!
Private Type Info
Name As String
Num1 As String
Num2 As String
End Type
Dim a() As Info
Dim FileName As String
FileName = "\1.ini" '修改ini文件名Private Sub Combo1_Click()
Text1(0).Text = a(Combo1.ListIndex).Name
Text1(1).Text = a(Combo1.ListIndex).Num1
Text1(2).Text = a(Combo1.ListIndex).Num2
End SubPrivate Sub Command1_Click(Index As Integer)
If Index = 0 Then
Open App.Path & FileName For Append As #1
Print #1, ""
Print #1, Trim(Text1(0).Text) & "|" & Trim(Text1(1).Text) & "|" & Trim(Text1(2).Text)
Close #1
a(UBound(a)).Name = Text1(0).Text
a(UBound(a)).Num1 = Text1(1).Text
a(UBound(a)).Num2 = Text1(2).Text
ReDim Preserve a(UBound(a) + 1)
MsgBox "添加成功"
Call RefreshIni
ElseIf Index = 1 Then
If Combo1.ListIndex >= 0 Then
a(Combo1.ListIndex).Name = Text1(0).Text
a(Combo1.ListIndex).Num1 = Text1(1).Text
a(Combo1.ListIndex).Num2 = Text1(2).Text
Open App.Path & FileName For Output As #1
Print #1, "[config]"
For i = 0 To UBound(a) - 1
Print #1, Trim(a(i).Name) & "|" & Trim(a(i).Num1) & "|" & Trim(a(i).Num2)
Next
Close #1
MsgBox "修改成功"
Call RefreshIni
End If
ElseIf Index = 2 Then
If Combo1.ListIndex >= 0 Then
For i = Combo1.ListIndex To UBound(a) - 2
a(i).Name = a(i + 1).Name
a(i).Num1 = a(i + 1).Num1
a(i).Num2 = a(i + 1).Num2
Next
ReDim Preserve a(UBound(a) - 1)
Open App.Path & FileName For Output As #1
Print #1, "[config]"
For i = 0 To UBound(a) - 1
Print #1, Trim(a(i).Name) & "|" & Trim(a(i).Num1) & "|" & Trim(a(i).Num2)
Next
Close #1
MsgBox "删除成功"
Call RefreshIni
End If
End IfEnd SubPrivate Sub RefreshIni()
Dim b() As String
ReDim Preserve a(0)
i = 0
Combo1.Clear
Text1(0).Text = ""
Text1(1).Text = ""
Text1(2).Text = ""
Open App.Path & FileName For Input As #1
Do While Not EOF(1)
Line Input #1, Tempstr
If InStr(Tempstr, "|") > 0 Then
b = Split(Tempstr, "|")
a(i).Name = b(0)
a(i).Num1 = b(1)
a(i).Num2 = b(2)
Combo1.AddItem a(i).Name
i = i + 1
ReDim Preserve a(i)
End If
Loop
Close #1End SubPrivate Sub Form_Load()Call RefreshIni
End Sub
请帮忙给改一下,只要能正常运行就可以!谢谢!
Private Type Info
Name As String
Num1 As String
Num2 As String
End Type
Dim a() As Info
Dim FileName As String
FileName = "\1.ini" '修改ini文件名Private Sub Combo1_Click()
Text1(0).Text = a(Combo1.ListIndex).Name
Text1(1).Text = a(Combo1.ListIndex).Num1
Text1(2).Text = a(Combo1.ListIndex).Num2
End SubPrivate Sub Command1_Click(Index As Integer)
If Index = 0 Then
Open App.Path & FileName For Append As #1
Print #1, ""
Print #1, Trim(Text1(0).Text) & "|" & Trim(Text1(1).Text) & "|" & Trim(Text1(2).Text)
Close #1
a(UBound(a)).Name = Text1(0).Text
a(UBound(a)).Num1 = Text1(1).Text
a(UBound(a)).Num2 = Text1(2).Text
ReDim Preserve a(UBound(a) + 1)
MsgBox "添加成功"
Call RefreshIni
ElseIf Index = 1 Then
If Combo1.ListIndex >= 0 Then
a(Combo1.ListIndex).Name = Text1(0).Text
a(Combo1.ListIndex).Num1 = Text1(1).Text
a(Combo1.ListIndex).Num2 = Text1(2).Text
Open App.Path & FileName For Output As #1
Print #1, "[config]"
For i = 0 To UBound(a) - 1
Print #1, Trim(a(i).Name) & "|" & Trim(a(i).Num1) & "|" & Trim(a(i).Num2)
Next
Close #1
MsgBox "修改成功"
Call RefreshIni
End If
ElseIf Index = 2 Then
If Combo1.ListIndex >= 0 Then
For i = Combo1.ListIndex To UBound(a) - 2
a(i).Name = a(i + 1).Name
a(i).Num1 = a(i + 1).Num1
a(i).Num2 = a(i + 1).Num2
Next
ReDim Preserve a(UBound(a) - 1)
Open App.Path & FileName For Output As #1
Print #1, "[config]"
For i = 0 To UBound(a) - 1
Print #1, Trim(a(i).Name) & "|" & Trim(a(i).Num1) & "|" & Trim(a(i).Num2)
Next
Close #1
MsgBox "删除成功"
Call RefreshIni
End If
End IfEnd SubPrivate Sub RefreshIni()
Dim b() As String
ReDim Preserve a(0)
i = 0
Combo1.Clear
Text1(0).Text = ""
Text1(1).Text = ""
Text1(2).Text = ""
Open App.Path & FileName For Input As #1
Do While Not EOF(1)
Line Input #1, Tempstr
If InStr(Tempstr, "|") > 0 Then
b = Split(Tempstr, "|")
a(i).Name = b(0)
a(i).Num1 = b(1)
a(i).Num2 = b(2)
Combo1.AddItem a(i).Name
i = i + 1
ReDim Preserve a(i)
End If
Loop
Close #1End SubPrivate Sub Form_Load()Call RefreshIni
End Sub
解决方案 »
- DataGrid行屬性是什么?
- 时间格式问题!
- 如何实现:www.XX.com有MSSQL数据库DB(含表Tab),要求写一个客户端软件,当表Tab中有记录插入时,在客户端C盘记录下该记录
- MDI问题,刚刚把100分的贴结了,再来开一个,解决立马给分!
- 用API实现窗体不能人为调节大小
- vb中怎么调用GetPrivateProfileString?
- 求救!
- VB程序员们:工作累了,别忘了休息,攒分去吧!!!
- 请高手指教,怎样在开始菜单中显示出Searching项,急!急!急!
- 请问如何在VB6中调用(执行)大型.exe;.com文件???
- 请教MSFlexGrid的单元格编辑的问题,多谢!!
- VB求助指点。万分感谢!
Dim a() As Info
Dim FileName As String
Private Type Info
Name As String
Num1 As String
Num2 As String
End TypePrivate Sub Combo1_Click()
Text1(0).Text = a(Combo1.ListIndex).Name
Text1(1).Text = a(Combo1.ListIndex).Num1
Text1(2).Text = a(Combo1.ListIndex).Num2
End SubPrivate Sub Command1_Click()
Dim Index As Integer
If Index = 0 Then
Open App.Path & FileName For Append As #1
Print #1, ""
Print #1, Trim(Text1(0).Text) & "|" & Trim(Text1(1).Text) & "|" & Trim(Text1(2).Text)
Close #1
a(UBound(a)).Name = Text1(0).Text
a(UBound(a)).Num1 = Text1(1).Text
a(UBound(a)).Num2 = Text1(2).Text
ReDim Preserve a(UBound(a) + 1)
MsgBox "添加成功"
Call RefreshIni
ElseIf Index = 1 Then
If Combo1.ListIndex >= 0 Then
a(Combo1.ListIndex).Name = Text1(0).Text
a(Combo1.ListIndex).Num1 = Text1(1).Text
a(Combo1.ListIndex).Num2 = Text1(2).Text
Open App.Path & FileName For Output As #1
Print #1, "[config]"
For i = 0 To UBound(a) - 1
Print #1, Trim(a(i).Name) & "|" & Trim(a(i).Num1) & "|" & Trim(a(i).Num2)
Next
Close #1
MsgBox "修改成功"
Call RefreshIni
End If
ElseIf Index = 2 Then
If Combo1.ListIndex >= 0 Then
For i = Combo1.ListIndex To UBound(a) - 2
a(i).Name = a(i + 1).Name
a(i).Num1 = a(i + 1).Num1
a(i).Num2 = a(i + 1).Num2
Next
ReDim Preserve a(UBound(a) - 1)
Open App.Path & FileName For Output As #1
Print #1, "[config]"
For i = 0 To UBound(a) - 1
Print #1, Trim(a(i).Name) & "|" & Trim(a(i).Num1) & "|" & Trim(a(i).Num2)
Next
Close #1
MsgBox "删除成功"
Call RefreshIni
End If
End IfEnd SubPrivate Sub RefreshIni()
On Error GoTo Err
Dim b() As String
ReDim Preserve a(0)
i = 0
Combo1.Clear
Text1(0).Text = ""
Text1(1).Text = ""
Text1(2).Text = ""
Open App.Path & FileName For Input As #1
Do While Not EOF(1)
Line Input #1, Tempstr
If InStr(Tempstr, "|") > 0 Then
b = Split(Tempstr, "|")
a(i).Name = b(0)
a(i).Num1 = b(1)
a(i).Num2 = b(2)
Combo1.AddItem a(i).Name
i = i + 1
ReDim Preserve a(i)
End If
Loop
Close #1
Exit SubErr:
MsgBox "文件不存在"
End SubPrivate Sub Form_Load()
FileName = "\1.ini" '修改ini文件名
Call RefreshIni
End Sub这样可以运行啦
别的没什么错误,只是我觉得代码写得不太好而已。
参考一下我给你修改的代码:
Option ExplicitPrivate Type Info
Name As String
Num1 As String
Num2 As String
End Type
Dim a() As Info
Dim FileName As String
' 不能在过程外对变量赋值啊,所以你写了这句,代码是错误的。
'FileName = "\1.ini" '修改ini文件名Private Sub Combo1_Click() Text1(0).Text = a(Combo1.ListIndex).Name
Text1(1).Text = a(Combo1.ListIndex).Num1
Text1(2).Text = a(Combo1.ListIndex).Num2End SubPrivate Sub Command1_Click(Index As Integer) Dim lTemp&, i& Select Case Index
Case 0:
lTemp = UBound(a) + 1
ReDim Preserve a(lTemp)
a(lTemp).Name = Trim$(Text1(0).Text)
a(lTemp).Num1 = Trim$(Text1(1).Text)
a(lTemp).Num2 = Trim$(Text1(2).Text)
Open App.Path & FileName For Append As #1
'Print #1, "" '没必要用这一句吧!
' 关键是你的原始文件最后一行要带回车换行符,即:用记事本打开后看起来有一空行!
Print #1, a(lTemp).Name; "|"; a(lTemp).Num1; "|"; a(lTemp).Num2
Close #1
Call RefreshIni
MsgBox "添加成功", 64
Case 1:
lTemp = Combo1.ListIndex
If (lTemp >= 0) Then
a(lTemp).Name = Trim$(Text1(0).Text)
a(lTemp).Num1 = Trim$(Text1(1).Text)
a(lTemp).Num2 = Trim$(Text1(2).Text)
Call SaveText
Call RefreshIni
MsgBox "修改成功", 64
End If
Case 2:
i = Combo1.ListIndex
If (i >= 0) Then
lTemp = UBound(a) - 1
For i = i To lTemp
a(i).Name = a(i + 1).Name
a(i).Num1 = a(i + 1).Num1
a(i).Num2 = a(i + 1).Num2
Next
ReDim Preserve a(lTemp)
Call SaveText
Call RefreshIni
MsgBox "删除成功", 64
End If
Case Else: MsgBox "还没有添加应该的事件代码!", 48
End SelectEnd SubPrivate Sub Form_Load() FileName = "\1.ini" '变量要在程序入口处初始化
Call RefreshIni(True)End Sub' ************************************************************Private Sub RefreshIni(Optional LoadData As Boolean = False) Dim b() As String, strTemp As String, i&
Combo1.Clear
Text1(0).Text = ""
Text1(1).Text = ""
Text1(2).Text = ""
If (LoadData) Then
i = -1
Open App.Path & FileName For Input As #1
Do While Not EOF(1)
Line Input #1, strTemp
If InStr(strTemp, "|") > 0 Then
i = i + 1: ReDim Preserve a(i)
b = Split(strTemp, "|")
a(i).Name = b(0)
a(i).Num1 = b(1)
a(i).Num2 = b(2)
Combo1.AddItem a(i).Name
End If
Loop
Close #1
Else
For i = 0 To UBound(a)
Combo1.AddItem a(i).Name
Next
End IfEnd SubPrivate Sub SaveText()
Dim i& Open App.Path & FileName For Output As #1
Print #1, "[config]"
For i = 0 To UBound(a)
Print #1, a(i).Name; "|"; a(i).Num1; "|"; a(i).Num2
Next
Close #1End Sub
'那天你的帖子结得够快的,晚上没事时写的,写完后,看到你的帖子已经结了。
'今天给你吧,仅供参考:Option Explicit
Dim iPath As String
Dim std() As StringPrivate Sub Combo1_Click()
Text1 = Split(std(Combo1.ListIndex), "|")(0)
Text2 = Split(std(Combo1.ListIndex), "|")(1)
Text3 = Split(std(Combo1.ListIndex), "|")(2)
End SubPrivate Sub Command1_Click() 'add
openFW2
End SubPrivate Sub Command2_Click() 'edit
If Text1 = "" Then
MsgBox "请选择条目"
Exit Sub
End If
std(Combo1.ListIndex) = Text1 & "|" & Text2 & "|" & Text3
openFW3
End SubPrivate Sub Command3_Click() 'del
Dim i As Integer
If Combo1.ListIndex = -1 Then
MsgBox "请选择条目"
Exit Sub
End If
For i = Combo1.ListIndex To Combo1.ListCount - 2
std(Combo1.ListIndex) = std(Combo1.ListIndex + 1)
Next
If UBound(std) = 0 Then
Combo1.Clear
Else
ReDim Preserve std(UBound(std) - 1)
End If
Text1 = "": Text2 = "": Text3 = ""
openFW3
End SubPrivate Sub Form_Load()
iPath = App.Path & "\my.ini" '//#########
Text1 = "": Text2 = ""
Text3 = "": Combo1.Text = ""
If Dir(iPath) <> "" Then
openFR
Else
openFW1
End If
End SubPrivate Sub openFR()
Dim myStr As String, i As Integer
ReDim std(0)
Combo1.Clear
Open App.Path & "\my.ini" For Input As #1
Line Input #1, myStr
While Not EOF(1)
Line Input #1, myStr
ReDim Preserve std(i)
std(i) = myStr
Combo1.AddItem Split(myStr, "|")(0)
i = i + 1
Wend
Combo1.Text = Text1
Close #1
End SubPrivate Sub openFW1()
Open App.Path & "\my.ini" For Output As #1
Print #1, "[config]"
Close #1End SubPrivate Sub openFW2()
Open App.Path & "\my.ini" For Append As #1
Print #1, Text1 & "|" & Text2 & "|" & Text3
Close #1
openFR
End SubPrivate Sub openFW3()
Open App.Path & "\my.ini" For Output As #1
Dim i As Integer
Print #1, "[config]"
If UBound(std) = 0 Then
Exit Sub
Close #1
End If
For i = 0 To UBound(std)
Print #1, std(i)
Next
Close #1
openFR
End Sub