-
This may seem like a really easy question for some of you, but I can't seem to get this...
I have a RichTextBox on my form and I want to save the text I write in it in a .ini file in a particular directory. I got it working with a normal text box, but for some reason it doesn't work for richtextboxes... I got this code from a site:
-------------------------
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
Const INIFileName = "C:\MyFile.ini"
------------------------------------
Private Sub Command1_Click()
'The following code line will save the text in Text1 under the section 'My App Name'
'and Under the Key 'My Key'.
Result = WritePrivateProfileString("My App Name", "My Key", Text1.Text, INIFileName)
End Sub
Private Sub Command2_Click()
Dim MyValue As String * 20
'The following code line will get the value of the key 'My Key' under the;section 'My App Name', and will insert the vale to the variable 'MyValue'.
Result = GetPrivateProfileString("My App Name", "My Key", "Empty", MyValue, Len(MyValue), INIFileName)
Text1 = MyValue
End Sub
=======================
This is the original code I got from the site, I just changed where it says "text1" to "Richtextbox1" and that all the changes I did to it...
Can anyone tell me what am I doing wrong or can you just show me a simpler way of doing this whole thing, for example using .txt instead of .ini?
Thanks!
-Emo
-
Try this:
Code:
Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Public 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
Public Function readini(strsection As String, strkey As String, strfullpath As String) As String
Dim strbuffer As String
Let strbuffer$ = String$(750, Chr$(0&))
Let readini$ = Left$(strbuffer$, GetPrivateProfileString(strsection$, ByVal LCase$(strkey$), "", strbuffer, Len(strbuffer), strfullpath$))
End Function
Public Sub writeini(strsection As String, strkey As String, strkeyvalue As String, strfullpath As String)
Call WritePrivateProfileString(strsection$, UCase$(strkey$), strkeyvalue$, strfullpath$)
End Sub
Usage:
RichTextBox1.text = readini("MyINI.ini", "Text", "C:\MyINI.ini")
Call writeini("MyINI.ini", "Text", "" & Text1.text & "", "C:\MyINI.ini")
-
Where do I put the...
Sorry, but where do I put the last two lines of your code, (after where is says "usage:")?
In the command buttons, or where?
Sorry, it's just that I'm not that advanced with VB...
Thanks
-Emo
-
Code:
1:
RichTextBox1.text = readini("MyINI.ini", "Text", "C:\MyINI.ini")
2:
Call writeini("MyINI.ini", "Text", "" & Text1.text & "", "C:\MyINI.ini")
The 1st code goes wherever/whenever you want to read it. Can go in a command button (called "read", maybe?) or in the form_load event.
And the same thing goes for the second code..wherever/whenever you want to write it (called "write", maybe?).
And the follwoing code:
Code:
Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Public 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
Public Function readini(strsection As String, strkey As String, strfullpath As String) As String
Dim strbuffer As String
Let strbuffer$ = String$(750, Chr$(0&))
Let readini$ = Left$(strbuffer$, GetPrivateProfileString(strsection$, ByVal LCase$(strkey$), "", strbuffer, Len(strbuffer), strfullpath$))
End Function
Public Sub writeini(strsection As String, strkey As String, strkeyvalue As String, strfullpath As String)
Call WritePrivateProfileString(strsection$, UCase$(strkey$), strkeyvalue$, strfullpath$)
End Sub
Goes into a module.
-
hmm...
Thanks a lot! I got it working