求助:VB读取、写入、修改、删除INI文件
INI文件如下:[config]王强|370123454567|36155陈飞|3701234132444|3545张明|3145676877990|6768胡亚楠|25...
INI文件如下:
[config]
王强|370123454567|36155
陈飞|3701234132444|3545
张明|3145676877990|6768
胡亚楠|254456677|14456
彭浩|46789|78999
........不知道会有多少个
有:一个Combo下拉菜单,3个text输入框、3个Command按钮
效果是:
Combo下拉菜单里只显示“王强,陈飞,张明.....”
点击里面的名字后,在text1显示"王强",在text2显示"370123454567",在text3显示"36155"
点击哪个名字,出现哪个名字的3个信息
3个Command按钮,分别是:写入、修改、删除
通过3个text输入框可写入新的信息,更新INI文件,在ini增加一行
修改3个text输入框目前显示的信息,更新INI文件
删除输入框目前显示的信息,更新INI文件
需要详细的代码,谢谢!!!
Command1的index属性设置为0吗?
Text1(0).Text = ""
Text1(1).Text = ""
Text1(2).Text = ""
请问怎么设置属性?运行老是提示错误 展开
[config]
王强|370123454567|36155
陈飞|3701234132444|3545
张明|3145676877990|6768
胡亚楠|254456677|14456
彭浩|46789|78999
........不知道会有多少个
有:一个Combo下拉菜单,3个text输入框、3个Command按钮
效果是:
Combo下拉菜单里只显示“王强,陈飞,张明.....”
点击里面的名字后,在text1显示"王强",在text2显示"370123454567",在text3显示"36155"
点击哪个名字,出现哪个名字的3个信息
3个Command按钮,分别是:写入、修改、删除
通过3个text输入框可写入新的信息,更新INI文件,在ini增加一行
修改3个text输入框目前显示的信息,更新INI文件
删除输入框目前显示的信息,更新INI文件
需要详细的代码,谢谢!!!
Command1的index属性设置为0吗?
Text1(0).Text = ""
Text1(1).Text = ""
Text1(2).Text = ""
请问怎么设置属性?运行老是提示错误 展开
3个回答
展开全部
汗、那个是为了方便你看的,你把它放到Formload里就行了,如下,改好的
同楼上问,这不是一个标准的ini文件,以下按楼主要求作的
按钮和文本框都用数组的
ini文件保存到同目录下,名字为1.ini,可修改
Private Type Info
Name As String
Num1 As String
Num2 As String
End Type
Dim a() As Info
Dim FileName As String
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 Sub
Private 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 If
End Sub
Private 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 #1
End Sub
Private Sub Form_Load()
FileName = "\1.ini" '修改ini文件名
Call RefreshIni
End Sub
同楼上问,这不是一个标准的ini文件,以下按楼主要求作的
按钮和文本框都用数组的
ini文件保存到同目录下,名字为1.ini,可修改
Private Type Info
Name As String
Num1 As String
Num2 As String
End Type
Dim a() As Info
Dim FileName As String
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 Sub
Private 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 If
End Sub
Private 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 #1
End Sub
Private Sub Form_Load()
FileName = "\1.ini" '修改ini文件名
Call RefreshIni
End Sub
展开全部
1、ini文件是文本文件,一般用于为应用程序提供参数。
2、ini文件既然是文本文件,vb6就可以把它当做文本文件用
open "d:\set.ini" For Input As #1来处理,但这样使
用并不方便。
3、windows API提供了GetPrivateProfileString和
WritePrivateProfileString来处理ini文件非常方便。
4、vb6下这两个API函数用法如下:
Option Explicit
'操作ini文件
Private Declare Function GetPrivateProfileString Lib "Kernel32" Alias "GetPrivateProfileStringA" _
(ByVal lpApplicationName As String, lpKeyName As Any, ByVal lpDefault As String, _
ByVal lpRetunedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "Kernel32" Alias "WritePrivateProfileStringA" _
(ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, _
ByVal lplFileName As String) As Long
Private r As Long
'从setup.ini读信息
Public Function GetMyINI(AppName As String, KeyName As String, FileName As String) As String
Dim RetStr As String
RetStr = String(255, Chr(0))
GetMyINI = Left(RetStr, GetPrivateProfileString(AppName, ByVal KeyName, "", _
RetStr, Len(RetStr), FileName))
End Function
'设置setup.ini
Public Sub SetMyINI(AppName As String, KeyName As String, entry As String, FileName As String)
r = WritePrivateProfileString(AppName, KeyName, entry, FileName)
If r <> 1 Then MsgBox "出错啦!"
End Sub
Private Sub Command1_Click()
'没有就创建一个set.ini
SetMyINI "监听端口", "端口", "11009", "d:\set.ini"
SetMyINI "要数周期", "周期", "60", "d:\set.ini"
SetMyINI "自动重新监听", "是否", "1", "d:\set.ini"
End Sub
Private Sub Command2_Click()
'存在就修改一下
SetMyINI "监听端口", "端口", "11010", "d:\set.ini"
End Sub
Private Sub Command3_Click()
'读取
MsgBox GetMyINI("监听端口", "端口", "d:\set.ini")
End Sub
Private Sub Command4_Click()
On Error Resume Next
'删除
Kill "d:\set.ini"
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
INI文件如下:
[config]
王强|370123454567|36155
陈飞|3701234132444|3545
张明|3145676877990|6768
胡亚楠|254456677|14456
彭浩|46789|78999
不是一个标准的ini文件 所以不能按照ini文件的读写方法.
你确定要如此吗?
[config]
王强|370123454567|36155
陈飞|3701234132444|3545
张明|3145676877990|6768
胡亚楠|254456677|14456
彭浩|46789|78999
不是一个标准的ini文件 所以不能按照ini文件的读写方法.
你确定要如此吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询