为了账号安全,请及时绑定邮箱和手机立即绑定

VB Combo循环加载INI文件?

/ 猿问

VB Combo循环加载INI文件?

跃然一笑 2019-04-09 10:06:21

VB Combo循环加载INI文件


查看完整描述

3 回答

?
哔哔one

假如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

'你附加那里没看明白你想表达什么.



查看完整回答
反对 回复 2019-04-13
?
HUX布斯

下面方法可以完美解决你遇到的问题(不管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


查看完整回答
反对 回复 2019-04-13

添加回答

回复

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信