Free Games Forum
Free Games Games Forums Music Forums TV Forums

  Free Games Forum Home FORUM
HOME
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN
Rules & FAQ RULES / FAQ
REPORT SPAM

Free Games Forum: Game Technology: Visual Basic / VB:
INI File Tutorial

 

 


Fire_Of_Hell
Newbie

Mar 24, 2006, 6:03 PM

Post #1 of 1 (220 views)
Shortcut
INI File Tutorial Can't Post

Needed:
a textbox named textbox1.
a button named savebutton
a class module named clsinifile (Click Project/Add Class Module)

Add this code in to the class module.




CODE:




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 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 WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long

Private Const BufferSize As Long = 4096

Private strIniFile As String

Public Property Get file() As String
file = strIniFile
End Property

Public Property Let file(Value As String)
strIniFile = Value
End Property

Public Function GetValue(strSection As String, strKey As String) As Variant
Dim strBuffer As String
Dim lLength As Long

strBuffer = Space(BufferSize)

lLength = GetPrivateProfileString(strSection, strKey, vbNullString, strBuffer, BufferSize, strIniFile)

GetValue = Left(strBuffer, lLength)
End Function

Public Sub WriteValue(strSection As String, strKey As String, vntValue As Variant)
WritePrivateProfileString strSection, strKey, CStr(vntValue), strIniFile
End Sub

Public Function GetSection(strSection As String) As Variant
Dim strBuffer As String
Dim lLength As Long

strBuffer = Space(BufferSize)

lLength = GetPrivateProfileSection(strSection, strBuffer, BufferSize, strIniFile)

GetSection = Split(Left(strBuffer, lLength), vbNullChar)
End Function

Public Function GetSectionKeys(strSection As String) As Variant
Dim strBuffer As String
Dim lLength As Long

strBuffer = Space(BufferSize)

lLength = GetPrivateProfileString(strSection, vbNullString, vbNullString, strBuffer, BufferSize, strIniFile)

GetSectionKeys = Split(Left(strBuffer, lLength), vbNullChar)
End Function





END CODE


Now We've Done the class ini file.

now all we need to do is code the form, this is quite easy.




CODE:




Private Sub Form_Load()
Dim Ini As ClsIniFile
Set Ini = New ClsIniFile

With Ini
.file = App.Path & "\INITest.ini"
INITest = .GetValue("INITest", "INITest")
End With

Textbox1.Text = INITest

End Sub

Private Sub Save()

Dim Ini As ClsIniFile
Set Ini = New ClsIniFile

With Ini
.file = App.Path & "\INITest.ini"
.WriteValue "INITest", "INITest", Textbox1.Text

End With

End Sub

Private Sub SaveButton_Click()
Call Save
End Sub





END CODE




That should do you, it will now make a file called INITest.ini in the directory that your program is in, once you click savebutton, it will then save textbox1.text in to the ini file, then when the form loads, it will load the text in the ini file in to the text boxWink.

This tutorial should help some people, cos when they modify the text they can make some pretty neat functions, for example, loading worlds on startup... e.g.




CODE:




if world = 115 then
loadworld (115)
end if

world = textbox1.text




END CODE




That code that i jsut stated was using a function called loadworld, for more information, go to sythe.org and go to their vb section, under dark knights scripts it will have a tute on making clients, and ini files, and a few more things, dark knights tute might be easier to follow... but im just trying to help.


plx rate my tutorialLaugh


(This post was edited by Fire_Of_Hell on Mar 25, 2006, 1:03 PM)


 
 
 


Search for (options) Web Design by Web Ideas - Page loaded in: 0.13 s on (CGI/1.1)