INI opslaan

Status
Niet open voor verdere reacties.

bartjuh94

Gebruiker
Lid geworden
16 aug 2007
Berichten
104
hoi iedereen,

Ik ben een spel aan het maken in visual basic. nu wil ik graag dat je kan opslaan en laden. Ik hoorde dat je het beste kon opslaan in ini bestanden. Hoe stel ik bv. een button in dat als je daar op klikt dat er dan een ini bestand word opgeslagen???
Bij voorbaat dank,

Bart Hoogmoed
info@bctonline.nl
www.bctonline.nl
 
van de 2e hit
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 WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long

' Functions
Function GetFromINI(sSection As String, sKey As String, sDefault As String, sIniFile As String)
    Dim sBuffer As String, lRet As Long
    ' Fill String with 255 spaces
    sBuffer = String$(255, 0)
    ' Call DLL
    lRet = GetPrivateProfileString(sSection, sKey, "", sBuffer, Len(sBuffer), sIniFile)
    If lRet = 0 Then
        ' DLL failed, save default
        If sDefault <> "" Then AddToINI sSection, sKey, sDefault, sIniFile
        GetFromINI = sDefault
    Else
        ' DLL successful
        ' return string
        GetFromINI = Left(sBuffer, InStr(sBuffer, Chr(0)) - 1)
    End If
End Function

' Returns True if successful. If section does not
' exist it creates it.
Function AddToINI(sSection As String, sKey As String, sValue As String, sIniFile As String) As Boolean
    Dim lRet As Long
    ' Call DLL
    lRet = WritePrivateProfileString(sSection, sKey, sValue, sIniFile)
    AddToINI = (lRet)
End Function

To save a value to the file, call the AddToINI function:

AddToINI "UserInfo", "Name", "James", App.Path & "settings.ini"

And to read values, simply call the GetFromINI function:

sValue = GetFromINI ("UserInfo", "Name", "", App.Path & "settings.ini"
 
waar moet ik deze code neerzetten??
in het form neem ik aan???
en wanneer word er opgeslagen... moet ik dan eerst een button hiervoor maken??
Bedankt voor uw reactie:)

Bart Hoogmoed
 
Je maakt eerst een MODULE en daarin zet je deze code:
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 WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long

' Functions
Function GetFromINI(sSection As String, sKey As String, sDefault As String, sIniFile As String)
    Dim sBuffer As String, lRet As Long
    ' Fill String with 255 spaces
    sBuffer = String$(255, 0)
    ' Call DLL
    lRet = GetPrivateProfileString(sSection, sKey, "", sBuffer, Len(sBuffer), sIniFile)
    If lRet = 0 Then
        ' DLL failed, save default
        If sDefault <> "" Then AddToINI sSection, sKey, sDefault, sIniFile
        GetFromINI = sDefault
    Else
        ' DLL successful
        ' return string
        GetFromINI = Left(sBuffer, InStr(sBuffer, Chr(0)) - 1)
    End If
End Function

' Returns True if successful. If section does not
' exist it creates it.
Function AddToINI(sSection As String, sKey As String, sValue As String, sIniFile As String) As Boolean
    Dim lRet As Long
    ' Call DLL
    lRet = WritePrivateProfileString(sSection, sKey, sValue, sIniFile)
    AddToINI = (lRet)
End Function



dan maak je 2 knoppen op je formulier
1e knop: zet de caption op "laad" en deze code er in:
Code:
sValue = GetFromINI ("UserInfo", "Name", "", App.Path & "settings.ini"
2e knop: zet op de caption "sla op" en zet deze code erin:
Code:
AddToINI "UserInfo", "Name", "James", App.Path & "settings.ini"


that's it (als je de parameters wel veranderd natuurlijk)
 
Ik krijg een paar fouten....
ini fouten.bmp

Heeft u hier nog een oplossing voor?
ik werk met visual basic 2008 en sommige functies werken hierin anders dan in oudere versies.
Met vriendelijke groet,

Bart Hoogmoed
www.bctonline.nl
 
weet heeft iemand een oplossing voor mijn probleempje???
ik wil graag ini opslaan in visual basic 2008 en de onderstaande code is vor een oudere versie denk ik
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan