vb 写入 读取 配置
本人就在大学里学了一点点VB,现在想实现“现有标签一数组28个,如某Caption值改变,则写入配置文件;再有一Command,点击则读取已写入的配置,显示于窗体之上”,...
本人就在大学里学了一点点VB,现在想实现“现有标签一数组28个,如某Caption值改变,则写入配置文件;再有一Command,点击则读取已写入的配置,显示于窗体之上”,求高人指点,能有完整代码100分拿走!!
展开
展开全部
'考虑一下用api函数来存取 ini文件?
Private 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
Private 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
'对api包装一下
'读写ini文件一个键
'忽略keyValue参数或空串为读取,传递非空参数测写入
'*.ini文件结构
' [Section]
' KeyName=KeyValue
' .........
Public Function ReadOrWriteInifile(ByVal Filename As String, ByVal Section As String, ByVal KeyName As String, Optional ByVal KeyValue As String)
Dim l As Long
If KeyValue = Empty Then
Dim buffer As String
buffer = String(1024, " ")
l = GetPrivateProfileString(Section, KeyName, "", buffer, LenB(buffer), Filename) '读取程序所在位置的某个Ini文件
If l > 0 Then
Dim Lc As Integer
Lc = InStr(1, buffer, Chr(0))
buffer = Mid(buffer, 1, Lc - 1)
Else
buffer = ""
End If
ReadOrWriteInifile = buffer
Else
l = WritePrivateProfileString(Section, KeyName, KeyValue, Filename) '写入到某个所在位置的Ini文件
ReadOrWriteInifile = CBool(l)
End If
End Function
Private Sub Command1_Click() '从配置文件读取label1数组的值
Dim item As Object
For Each item In Label1 '循环label1数组每个控件
item.Caption = ReadOrWriteInifile(App.Path & "\config.ini", "label1", item.Index)
Next
End Sub
Private Sub Form_Unload(Cancel As Integer) '窗体卸载时一次性把label1数组值写入配置文件
Dim item As Object
For Each item In Label1
ReadOrWriteInifile App.Path & "\config.ini", "label1", item.Index, item.Caption
Next
End Sub
Private Sub Label1_Change(Index As Integer) '写入配置文件
ReadOrWriteInifile App.Path & "\config.ini", "label1", Index, Label1(Index).Caption
'config.ini文件结构:
'[Label1]
'1="label1(1)的值"
'2="label1(2)的值"
End Sub
2014-02-19 · 知道合伙人互联网行家
关注
展开全部
Private Sub Command1_Click() '读取文件,显示出来
On Error GoTo endLine
Dim s As String, ss() As String, i As Integer
Open "c:\data.txt" For Input As #1
readline 1, s
Close
If InStr(s, "|") Then
ss = Split(s, "|") '分割
For i = 0 To UBound(lbl)
lbl(i).Caption = ss(i) '显示在lbl控件中
Next i
End If
endLine:
End Sub
Private Sub lbl_Change(Index As Integer) '保存到文件中
On Error Resume Next
Dim s As String, i As Integer
For i = 0 To UBound(lbl) '把所有caption放到s变量中,用竖线分隔开来
s = s & lbl(i).Caption & "|"
Next i
Open "c:\data.txt" For Output As #2 '写文件
Print #2, s
Close
End Sub
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询