问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

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
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
sometimesome timesometimessome times的区别 瓜地的午餐怎么造句 夏季宝宝不吃饭应该怎么办 狼人杀里面的白狼王模式怎么玩? 狼人杀白狼王和骑士:操作至上的高效玩法指南 狼人杀手游白狼王怎么玩 详细玩法攻略 狼人杀 狼人杀的白狼王怎么玩? 国产轮毂品牌有哪些 国产轮毂品牌哪个品牌好 国内轮毂有哪些品牌 岗位和职务怎么填 大唐不夜城离小南门早市多远 昼夜更替是由地球的哪种运动引起的自然现象? 新密市下庄河制药厂地址 神兵乱的txt全集下载地址 河南宛西制药厂在哪里 在三角函数中,三角函数在什么象限内,是正的? 从遵义湄潭出发去湄江大峡谷怎么走法? 湄潭县西河镇小河电站装机容量多大 湄江的水利工程 飞机上宝宝吃什么 电脑五笔打字“疍”字怎么打? ...学习过程中遇到点字用五笔打不出来,比如:祐、弌、仹。 这样的字可以打出来么?怎么打?? 《弌》字手机怎么打出来??急! 从龙岩长汀往福州走高速怎么走?谢谢 龙岩到福州大学城具体路线 福州自驾到南宁龙州花山岩经过哪里 龙岩到福州多少公里 龙岩开车到福州衣锦坊哪条线路好 新股申购和账户所需i资金有关吗 新买的mac pro,装了win7,下软件时突然发现C盘多了很多eula,install.r... 红星照耀中国中红军对基督教徒和民团俘虏的态度是如何的 张洪波的人物履历 架子鼓音箱与普通音箱的区别 想要不停的快乐,就给我砰砰砰,想要。。。就给我砰砰砰。。。就这么几... 教体局内职工都是什么身份?是教师编制吗 严重亏电的启停电瓶修复时充电特别慢正常吗 斐济属于什么国家 《后形而上学希望新实用主义社会、政治和法律哲学》pdf下载在线阅读,求... 枣庄中央广场三角花园在哪里? 数列前N项和比值问题 等比数列{an}的前n项和是Sn 若S6比S3=3,则S9比... K412列车02号车厢004号是否靠窗 微信电脑版锁定功能在哪里 原神无法跟npc对话怎么办 为什么和兰罗摩对话不了 二十一个国家的语言说:生日快乐。 qq炫舞安装包怎么删除 交通船能有28客位么 小船是交通工具吗 游戏王tcg卡片欧版和美版和亚英的区别