It is not necessarily .ini files. The extension may vary, depends on the developer will, but .ini is the most commonly used extension.
It's actually quite simple, with the utilization of API. API is very useful in VB. It can do a lot of things can't be done using classical VB components.
1. Make a text files with the format specified below: name it test.ini, and put it in the same folder where the application is made.
[Header_Key1]
key1=value1
key2=value2
...
[Header_Key2]
key3=value3
...
where:
Header_Key1, Header_Key2 are the keys used for a group of keys.
key1, key2, key3 are the keys of the values to be read and written.
e.g. (To get current period and semester from text files)
[Period]
Period=2009
Semester=2
[Content]
AnotherContent=this is that other content
2. Make a module and put the following functions (to read and write text files) into the module:
Option Explicit
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
Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" _
(ByVal lpApplicationname As String, ByVal lpKeyName As Any, ByVal lsString As String, ByVal lplFilename As String) As Long
Function GetfromINI(SectionHeader$, VarName$, FileName$) As String
Dim RetStr As String
RetStr = String(255, Chr(0))
GetfromINI = Left(RetStr, GetPrivateProfileString(SectionHeader$, ByVal VarName$, "", RetStr, Len(RetStr), FileName$))
End Function
3. To get the value of the keys on the .ini files :
Objects used:
3 textboxes named Text1, Text2, Text3
Dim isi1 As String, isi2 As String, isi3 As String
isi1 = GetfromINI("Period", "Period", App.Path + "\test.ini")
isi2 = GetfromINI("Period", "Semester", App.Path + "\test.ini")
isi3 = GetfromINI("Content", "AnotherContent", App.Path + "\test.ini")
Text1.Text = isi1
Text2.Text = isi2
Text3.Text = isi3
4. To save the value on textbox to the .ini files :
Objects used:
3 textboxes named Text1, Text2, Text3
WritePrivateProfileString "Period", "Period", Text1.Text, App.Path + "\test.ini"
WritePrivateProfileString "Period", "Semester", Text2.Text, App.Path + "\test.ini"
WritePrivateProfileString "Content", "AnotherContent", Text3.Text, App.Path + "\test.ini"
That's all.
0 comments:
Post a Comment