VB Combo循环加载INI文件
发布网友
发布时间:2023-08-24 11:10
我来回答
共5个回答
热心网友
时间:2023-08-24 17:39
说老实话,没有明白你的意思。
以下面举例:
[Section]
1000000000=111
123456789=222
abcdefg=333
中文名称=444
这个是一个ini文件,你的要求是不是这样:
有两个combo,一个里面加载所有的Section,一个里面加载选中Section里面的所有Keyname,还有一个文本,里面加载选中keyname的key值??
麻烦说清楚哦~~·想赚分。
还有哦,删除的话,是删除KeyName还是Key值??
贴上代码,看看是否符合要求
--------------------以下(此行不用复制)----------
'ini文件放在同一当前工作目录中,并且名称为:Test.ini
'向窗体中添加2个ComboBox,名称分别为comS和comN
'向窗体中添加2个CommandButton,名称分别为cmdF和cmdD
'下面的代码放到窗体之中
Dim strIniPath As String
'刷新KeyName
Private Sub RefreshcomN()
If comS.Text <> "" Then
KeyName = GetKeyName(comS.Text, strIniPath)
comN.Clear
For j = LBound(KeyName) To UBound(KeyName)
comN.AddItem KeyName(j)
Next
End If
End Sub
'刷新Section
Private Sub RefreshComs()
comS.Clear
Dim i As Integer, j As Integer
Dim Sec() As String, KeyName() As String
Sec = GetSection(strIniPath)
For i = LBound(Sec) To UBound(Sec)
comS.AddItem Sec(i)
Next
comN.Clear
End Sub
Private Sub cmdD_Click() '删除Section或者KeyName
If comS.Text = "" Then
MsgBox "请选择要删除的字段!"
Else
If comN.Text = "" Then
If MsgBox("是否删除Section:" & comS.Text, vbYesNo, "提示") = vbYes Then
DelSection comS.Text, strIniPath
cmdF_Click
comN.Clear
End If
End If
If comN.Text <> "" Then
If MsgBox("是否删除" & comS.Text & "中的KeyName:" & comN.Text, vbYesNo, "提示") = vbYes Then
DelKeyName comS.Text, comN.Text, strIniPath
RefreshcomN
End If
End If
End If
End Sub
Private Sub cmdF_Click()
RefreshComs
End Sub
Private Sub comS_Click()
RefreshcomN
End Sub
Private Sub Form_Load()
strIniPath = App.Path & "\Test.ini"
comS.Clear '清除Coms中的Section记录
RefreshComs
comN.Clear '清除comN中的KeyName记录
End Sub
--------------------以上(此行不用复制)----------
--------------------以下(此行不用复制)----------
'新建一模块,下面的代码放到模块当中
Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" ( _
ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, _
ByVal lpDefault As String, _
ByVal lpReturnedString As String, _
ByVal nSize As Long, _
ByVal lpFileName As String _
) As Long
Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" ( _
ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, _
ByVal lpString As Any, _
ByVal lpFileName As String _
) As Long
'获取ini文件中的Section
Public Function GetSection(IniFilePath As String) As Variant
Const ProStringLen = 1024
Dim Res As Long
Dim S As String
Dim i As Long
S = Space(ProStringLen)
Res = GetPrivateProfileString(vbNullString, vbNullString, vbNullString, S, ProStringLen, IniFilePath)
S = Trim(Left(S, Res))
If S <> "" Then
i = Len(S)
Do While Mid(S, i, 1) = vbNullChar
i = i - 1
Loop
S = Left(S, i)
End If
GetSection = Split(Trim(S), vbNullChar)
End Function
'获取ini文件中的KeyName
Public Function GetKeyName(Section As String, IniFilePath As String) As Variant
Const ProStringLen = 1024
Dim Res As Long
Dim S As String
Dim i As Long
S = Space(ProStringLen)
Res = GetPrivateProfileString(Section, vbNullString, vbNullString, S, ProStringLen, IniFilePath)
S = Trim(Left(S, Res))
If S <> "" Then
i = Len(S)
Do While Mid(S, i, 1) = vbNullChar
i = i - 1
Loop
S = Left(S, i)
End If
GetKeyName = Split(Trim(S), vbNullChar)
End Function
'删除指定Section段内的数据
Public Sub DelSection(Section As String, IniFilePath As String)
WritePrivateProfileString Section, vbNullString, vbNullString, IniFilePath
End Sub
'删除指定KeyName的数据
Public Sub DelKeyName(Section As String, KeyName As String, IniFilePath As String)
WritePrivateProfileString Section, KeyName, vbNullString, IniFilePath
End Sub
--------------------以上(此行不用复制)----------
热心网友
时间:2023-08-24 17:40
Private Declare Function icePub_getIniString Lib "icePubDll.dll" (ByVal strDefaultValue As String, ByVal strGroupName As String, ByVal strKeyName As String, ByVal strIniFilename As String, ByVal strResult As String) As Integer
Private Declare Function icePub_getIniValue Lib "icePubDll.dll" (ByVal valDefaultValue As Integer, ByVal strGroupName As String, ByVal strKeyName As String, ByVal strIniFilename As String) As Integer
Private Declare Function icePub_setIniString Lib "icePubDll.dll" (ByVal strValue As String, ByVal strGroupName As String, ByVal strKeyName As String, ByVal strIniFilename As String) As Integer
Dim val2 As Integer
Dim strVal As String
For i = 1 To 5
keyName = Key + Trim(Str(i))
'值
val2 = icePub_getIniValue(0, "Section", keyName, App.Path + "\myini.ini")
'串
strVal = icePub_getIniValue(0, "Section", keyName, App.Path + "\myini.ini")
'往Combo1里添加
Next i
'写
a = icePub_setIniString("555", "Section", "Key1", App.Path + "\myini.ini")
http://zhidao.baidu.com/question/139238142.html
里边下载的rar包里有dll
热心网友
时间:2023-08-24 17:40
假如INI的内容为:
[Section1]
Key1=111
Key2=222
Key3=333
[Section]
Key1=111
Key2=222
Key3=333
[Section2]
Key1=111
Key2=222
Key3=333
Private Sub Combo1_Click()
Text1 = Mid(Combo1.Text, 6, Combo1.ListCount)
End Sub
Private Sub Command1_Click()
Open "C:\Documents and Settings\MoBin\桌面\a.ini" For Input As #1 '你自己改一下你的ini的路径
Do While Not EOF(1)
Line Input #1, ini
If ini = "[Section]" Then
Do
Line Input #1, ini1
Combo1.AddItem ini1
Loop While (Left(ini1, 3) = "Key")
Combo1.RemoveItem Combo1.ListCount - 1
Combo1.Text = Combo1.List(0)
Exit Do
End If
Loop
Close #1
End Sub
'你附加那里没看明白你想表达什么.
热心网友
时间:2023-08-24 17:41
Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, addr As Byte, ByVal nSize As Long, ByVal lpFileName As String) As Long
'section下的所有项存于数组
Private TString() As String
Private SectionName As String
Private iniFileName As String
Public Function GetINISection(strSection As String)
Dim MyKeys As String * 10000 '设置一个极大的数字
Dim EachElement
Dim EachKey
SectionName = strSection
GetPrivateProfileSection strSection, MyKeys, 10000, iniFileName
ReDim TString(0)
EachKey = Split(MyKeys, Chr(0))
For Each EachElement In EachKey
If EachElement = "" Then
Exit For
End If
TString(UBound(TString)) = EachElement
ReDim Preserve TString(UBound(TString) + 1)
Next
End Function
Public Function GetINIStr(strSection As String, StrKey As String) As String
Dim tmpStr(256) As Byte
returnval = GetPrivateProfileString(strSection, StrKey, "", tmpStr(0), 256, iniFileName)
GetINIStr = StrConv(tmpStr, vbUnicode)
End Function
'***************************************************************
Private Sub Combo1_Click()
Text1 = GetINIStr(SectionName, Combo1.Text)
End Sub
Private Sub Form_Load()
Dim i As Long, StrT() As String
'设置ini
iniFileName = App.Path & "\1.ini"
GetINISection ("section")
For i = 0 To UBound(TString) - 1
StrT = Split(TString(i), "=")
'这里显示等号左边的数字
Combo1.AddItem StrT(0)
Next i
Combo1.ListIndex = 0
End Sub
=================================
还有一个delinistr我懒得做了,你可以去查一下api,仿照getinistr自己写一个出来。。
准备工作,建立窗口,建一个combo1,text1
热心网友
时间:2023-08-24 17:41
下面方法可以完美解决你遇到的问题(不管Section下的键有没有规律):
一、建立一个模块,复制下面代码:
Option Explicit
Declare Function GetPrivateProfileStringByKeyName& Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName$, ByVal lpszKey$, ByVal lpszDefault$, ByVal lpszReturnBuffer$, ByVal cchReturnBuffer&, ByVal lpszFile$)
Declare Function GetPrivateProfileStringKeys& Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName$, ByVal lpszKey&, ByVal lpszDefault$, ByVal lpszReturnBuffer$, ByVal cchReturnBuffer&, ByVal lpszFile$)
Declare Function WritePrivateProfileStringToDeleteKey& Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As Long, ByVal lplFileName As String)
Const MAXKD = 2048
Private rtn As String
Private success As String
Function GetPrivateStringValue(section$, Key$, File$) As String
Dim KeyValue$
Dim characters As Long
KeyValue$ = String$(MAXKD, 0)
characters = GetPrivateProfileStringByKeyName(section$, Key$, "", KeyValue$, Len(KeyValue$) - 1, File$)
If characters > 1 Then
KeyValue$ = StrConv(LeftB(StrConv(KeyValue$, vbFromUnicode), characters), vbUnicode)
End If
GetPrivateStringValue = KeyValue$
End Function
Function GetIniFile_EX(IniFilename As String, ByVal lpApplicationName$, ByVal lpszKey$, OutPutArray() As String) As String
ReDim OutPutArray(0)
rtn = String$(MAXKD, 0)
If lpApplicationName <> "" And lpszKey$ = "" Then
success = GetPrivateProfileStringKeys(lpApplicationName$, 0, "", rtn, Len(rtn), IniFilename) 'get the entries from the section
Else
OutPutArray(0) = GetPrivateStringValue(lpApplicationName$, lpszKey$, IniFilename)
GetIniFile_EX = OutPutArray(0)
Exit Function
End If
Dim NullOffset%
Do
NullOffset% = InStr(rtn, Chr$(0))
If NullOffset% > 1 Then
OutPutArray(UBound(OutPutArray)) = Mid$(rtn, 1, NullOffset% - 1)
rtn = Mid$(rtn, NullOffset% + 1)
ReDim Preserve OutPutArray(UBound(OutPutArray) + 1)
End If
Loop While NullOffset% > 1
If UBound(OutPutArray) > 0 Then ReDim Preserve OutPutArray(UBound(OutPutArray) - 1)
GetIniFile_EX = OutPutArray(0)
End Function
二、在窗体内加入控件combo1,text1,command1,然后复制下面代码(注意修改ini文件名),运行即可。
Option Explicit
Private Sub Combo1_Click()
Dim S() As String
GetIniFile_EX App.Path & "\a.ini", "Section", Combo1.Text, S
Text1.Text = S(0)
End Sub
Private Sub Form_Load()
Dim i As Integer
Dim S() As String
GetIniFile_EX App.Path & "\a.ini", "Section", "", S
Combo1.Clear
For i = 0 To UBound(S)
Combo1.AddItem S(i)
Next
Combo1.ListIndex = 0
End Sub
Private Sub Command1_Click()
WritePrivateProfileStringToDeleteKey "Section", Combo1.Text, 0&, App.Path & "\a.ini"
Combo1.ListIndex = 0
End Sub