Saving Other Things -- Foreground Color and other things

paidelr
12-01-2004, 04:48 PM
So what I want to do is save other things like the foreground and background colors of a label, the background that I have set on my form, the picture I'm using in my command button. I want to save it to a dat file, we'll call it test.dat.

Any help?

Diurnal
12-01-2004, 05:26 PM
You can use standard File I/O (http://www.visualbasicforum.com/showthread.php?t=123814&highlight=file) or ini files using GetPrivateProfileString and WritePrivateProfileString API's. If you are just doing it once, I would probably use the File I/O method. If you plan on doing more, take the time to learn the API's and write a wrapper class to do it for you. The ini files are easy and work well.

paidelr
12-01-2004, 05:35 PM
I can do it using:

Dim a as string
Open test.dat as Output #1
Print #1, a: label1.background = a

? Never knew that...

paidelr
12-02-2004, 02:52 PM
Here is my code:

Private Sub Command2_Click()
'Saving the information

Open App.Path & "skin.dat" For Output As #1
Write #1, options.Picture
Write #1, options.preview.BackColor
Write #1, options.preview.Forecolor
Write #1, options.preview2.Picture
Close #1
MsgBox ("Data Saved.")
End Sub

Form_Load()

'Load the information

Dim a As String, b As String, c As String, d As String

a = options.Picture
b = options.preview.BackColor
c = options.preview.Forecolor
d = options.preview2.Picture

Open App.Path & "\skin.dat" For Input As #1
Do Until EOF(1)
Input #1, a
Input #1, b
Input #1, c
Input #1, d
Loop
Close #1


options is my form where I'm using. preview is a LABEL while preview2 is a CommandButton.

I'm pretty sure that the problem is because my information isn't SAVING. When I go into my skin.dat file, no information is inside.Help?

paidelr
12-02-2004, 03:07 PM
Fixed one error. I forgot a "\" beside the filename when I was saving the file. Now that works. This is the data in my skin.dat file:

"22662"
4194368
49344
"22698"


The problem is that when I load back up the data, it doesn't load back in. Any ideas. Is there a fault in my code for loading back the data?

Diurnal
12-02-2004, 04:09 PM
I think you have it backwards. When you want to load the information, retrieve it from the file and then assign it to the property you need to set:

Private Sub Form_Load()
'Load the information

Dim a As String, b As String, c As String, d As String

'Don't need these:
' a = Options.Picture
' b = Options.preview.BackColor
' c = Options.preview.ForeColor
' d = Options.preview2.Picture

Open App.Path & "\skin.dat" For Input As #1
Input #1, a, b, c, d
Close #1

'Use the variables to set the properties.
Options.preview.BackColor = CLng(b)
Options.preview.ForeColor = CLng(c)

'these pobably need to be the path to a file to load:
'Options.Picture = LoadPicture("Some file and path")
Options.Picture = CLng(a)
Options.preview2.Picture = CLng(d)

End Sub

paidelr
12-02-2004, 04:39 PM
Yup, I need a file path in the bottom two. When I'm saving to my file, I'm trying the following:

Write #1, "\" & Label4.text & ".jpg"
Write #1, "\" & Label5.text & ".jpg"




And then I'm loading it back with:

options.Picture = LoadPicture("\" & a & ".jpg") = CLng(a)
options.preview2.Picture = LoadPicture("\" & d & ".jpg") = CLng(d)

But I get a 'type mismatch' error on the '=' sign right before the CLng.

paidelr
12-02-2004, 04:46 PM
Ok I fixed the above problem.

options.Picture = LoadPicture(a)

Now it says that my file (\bgred.jpg) doesn't exist but it is in my folder!

MikeJ
12-02-2004, 06:00 PM
If it's in your folder, you need to use App.Path. It expects a full file path, not a relative one:

Dim filepath As String
'Make sure it's not in a root directory
If Right$(App.Path) = "\" Then
'If it is, it won't like a trailing slash
filepath = App.Path & "bgred.jpg"
Else
'However, if it isn't, it'll need one
filepath = App.Path & "\bgred.jpg"
End If

'Do whatever with your filepath variable
MsgBox filepath
Options.Picture = LoadPicture(filepath)

Better yet, you could build a function where you pass the name of the file, and it will build a string for you (IE - check for the trailing slash in the function and return the appropriate value).

paidelr
12-02-2004, 08:55 PM
Yup, that's fixed. Another error has popped up:

When I save my settings and run my whole program again, everything works fine and shows up great.

Then I click to go to Form2 and everything has my default settings on them (for forecolor, backcolor, picture, etc...). When I go back to Form1 and then back into Form2, everything works fine.

I thought it was because I wasn't loading the Form but I tried doing that, tried loading and unloading, hiding it, showing it, hiding it on form_load, etc... but nothing. I'm gonna play with it but if you have any ideas, I'd love to hear them.

Diurnal
12-03-2004, 08:32 AM
What code are you running when you load the second form? Can you set a break point and step through the code when you load this form? Do the correct variables get read and set before the form is shown?

paidelr
12-03-2004, 02:35 PM
Ahh, I realize why it's doing thing. When I run my program (hitting F5 in VB), my form isn't actually activated and my code is in the form_activate form so when I go into Form2 and then Form1, Form1 is actiavet and thus, the code runs.

If I put the code in Form_Load() it doesn't work at all (probably because the form doesn't load up).

black diamond
12-06-2004, 01:34 AM
use as a class module


'Quasar File Use
'black diamond at gmail dot com
'use at your will, just keep a little tiny credit to me
'what it really does is managing ini files ;)

Option Explicit

Public FileName As String
Public DontSaveVoid As Boolean
Dim Entries As Integer

Dim Entry(0 To 100) As String
Dim Value(1 To 100, 0 To 100) As Variant

Public Function EntriesN() As Integer
EntriesN = Entries
End Function

Public Sub Save()
SaveToFile FileName
End Sub

Public Sub SaveToFile(fName As String)

Dim gfn As String
If Len(gfn) = 0 Then
gfn = FileName
End If
If Len(gfn) > 0 Then

Dim eNt As Integer
Dim vAl As Integer
Open fName For Output As #1
For eNt = 1 To Entries
Print #1, "[" & Entry(eNt) & "]"

For vAl = 1 To Value(eNt, 0)
If DontSaveVoid = False Then
Print #1, Value(eNt, vAl)
End If
If DontSaveVoid = True And InStr(1, Value(eNt, vAl), "=", vbTextCompare) <> Len(Value(eNt, vAl)) Then
Print #1, Value(eNt, vAl)
End If
Next vAl
Next eNt
Close #1

End If
End Sub

Public Sub Load()
LoadFromFile FileName
End Sub

Public Sub LoadFromFile(fName As String)
Dim f As Integer
Dim i As Integer
Dim tMp As String
f = FreeFile

Entries = 0
Open fName For Input As f
i = 0
Do While Not EOF(f) And i <= 100
Line Input #f, tMp
i = i + 1
If (InStr(1, tMp, "[", vbTextCompare) = 1) And (InStr(1, tMp, "]", vbTextCompare) = Len(tMp)) Then
Entries = Entries + 1
Value(Entries, 0) = 0
Entry(Entries) = Mid(tMp, 2, Len(tMp) - 2)
End If
If InStr(2, tMp, "=", vbTextCompare) > 0 And Entries > 0 Then
Value(Entries, 0) = Value(Entries, 0) + 1
Value(Entries, Value(Entries, 0)) = tMp
End If
Loop
Close f
End Sub

Public Sub CheckValue(gEntry As String, gValue As String, gSet As String)
If Len(GetValue(gEntry, gValue)) < 1 Then
SetValue gEntry, gValue, gSet
End If
End Sub

Public Sub SetValue(gEntry As String, gValue As String, gSet As String)
Dim ggEntry As String, ggValue As String, ggSet As String
ggEntry = gEntry
ggValue = gValue
ggSet = gSet

ggEntry = gEntry
ggValue = gValue
Dim eNt As Integer, i As Integer
eNt = 0
For i = 1 To Entries
If Entry(i) = ggEntry Then
eNt = i
End If
Next i
Dim tMp
Dim done As Boolean
done = False
If eNt = 0 Then
Entries = Entries + 1
Entry(Entries) = ggEntry
Value(Entries, 0) = 1
Value(Entries, 1) = ggValue & "=" & ggSet
Else

For i = 1 To Value(eNt, 0)
tMp = Split(Value(eNt, i), "=")
If tMp(0) = ggValue Then
Value(eNt, i) = ggValue & "=" & ggSet
done = True
End If
Next i

If done = False Then
Value(eNt, 0) = Value(eNt, 0) + 1
Value(eNt, Value(eNt, 0)) = ggValue & "=" & ggSet
End If
End If
End Sub

Public Sub EraseValue(gEntry As String, gValue As String)
SetValue gEntry, gValue, ""
End Sub

Public Function GetValue(gEntry As String, gValue As String) As Variant
Dim ggEntry As String
Dim ggValue As String
ggEntry = gEntry
ggValue = gValue
Dim eNt As Integer, i As Integer
eNt = 0
For i = 1 To Entries
If Entry(i) = ggEntry Then
eNt = i
End If
Next i
Dim tMp
If eNt = 0 Then
GetValue = ""
Else
For i = 1 To Value(eNt, 0)
tMp = Split(Value(eNt, i), "=")
If tMp(0) = ggValue Then
GetValue = tMp(1)
End If
Next i
End If
End Function



sample usage

Private Sub Form_Load()
x.FileName = App.Path & "\settings.ini"
x.Load
x.CheckValue "Window", "Height", "300"
x.CheckValue "Window", "Width", "850"
x.Save 'optional
End Sub

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum