I'm getting back into VB6 a little for a project I'm working on right now. I was never great at it in the first place as my knowledge never really got past the beginner stage (lack of time rather than lack of effort) and I only gained experience in a small corner of VB6, but I'm pretty much stuck with a few things now. Forgive me if my questions are incredibly simple, but the lack of experience in doing certain things is telling now.
I'm writing a program which, for a few reasons, must be able to allow the the program's colours to be edited. So, in short, I've created an Options form where the user can pick colours for the form background, textboxes, text, label background, label text and command buttons. When the user clicks the save button, this is then saved to a text file named prefs.txt, using the below code;
Code:
Private Sub cmdSave_Click()
Dim sFileText As String
Dim iFileNo As Integer
iFileNo = FreeFile
' Open the file for writing
Open (App.Path & "\prefs.txt") For Output As #iFileNo
' Write form colour
Print #iFileNo, frmMain.BackColor
' Write textbox colour
Print #iFileNo, frmMain.txtForename.BackColor
' Write text colour
Print #iFileNo, frmMain.txtForename.ForeColor
' Write label colour
Print #iFileNo, frmMain.lblForename.BackColor
' Write label text colour
Print #iFileNo, frmMain.lblForename.ForeColor
' Close the file
Close #iFileNo
MsgBox (Saved)
End Sub
This results in the file being saved in the following format;
Code:
0
255
8388608
65535
12615935
The part I'm stuck on is reading the data when the program is launched to set the colours to the appropriate object. I've tried using my Google-fu to find a solution, but can't seem to find one which can handle the new lines. I've added a bit of code to show the file's data in Form_Load as follows;
Code:
Dim tmp As String, contents As String
Open (App.Path & "\prefs.txt") For Input As #1
While EOF(1) = 0
Line Input #1, tmp
contents = contents + tmp
Wend
Close #1
MsgBox contents
But actually having the program read the file and setting the object colours is the sticking point. For what it's worth, I'm using common dialogs to allow the user to set the object colours; the form colour is edited when the user clicks a command button, which runs the following code;
Code:
Private Sub cmdFormEdit_Click()
' Set Cancel to True
cmdlgForm.CancelError = True
On Error GoTo ErrHandler
' Set the Flags property
cmdlgForm.Flags = cdlCCRGBInit
' Display the Colour Dialog box
cmdlgForm.ShowColor
' Set the form's background colour to selected colour
frmMain.BackColor = cmdlgForm.Color
frmEdit.BackColor = cmdlgForm.Color
frmOptions.BackColor = cmdlgForm.Color
frmSearch.BackColor = cmdlgForm.Color
Exit Sub
ErrHandler:
' User pressed the Cancel button
End Sub
I have a few other questions as well, but they don't relate to file handling, so I'll ask them in the appropriate sub-forum.
Rather than use Print to write your data I would use Write.
Write is used to write data to a file.
Print is used to print text to a file.
For the colors, it probably doesn't make much difference.
That file I/O mode is old as QBASIC (and before) and normally you would have two, nearly identical sections of code, one for writing the file, and one for reading.
You just need to input in the same order that you output, i.e.
Write #FF, LastName, FirstName, MiddleName
Write #FF, Age, PhoneNumber, City
Write #FF, This
Write #FF, That
On the read side, you would have
Input #FF, LastName, FirstName, MiddleName
Input #FF, Age, PhoneNumber, City
Input #FF, This
Input #FF, That
Unfortunately, since you want to save and read object's properties, you can't do the read as simply.
While an object will return a property value, similarly to a function call, you can't set the property through Input. Input needs a standard VB type.
So, as an example, you can write to the file like this.
' Open the file for writing
iFileNo = FreeFile
Open (App.Path & "\prefs.txt") For Output As #iFileNo
' Write form colour, width and height
Write #iFileNo, frmMain.BackColor, frmMain.Width, frmMain.Height
But you have to read it like this.
Dim inputVal As Long
Dim inputstr As String
Dim iFileNo As Integer
' Open the file for writing
iFileNo = FreeFile
Open (App.Path & "\prefs.txt") For Input As #iFileNo
' Read form colour, width and height
Input #iFileNo, inputVal: frmMain.BackColor = inputVal
Input #iFileNo, inputVal: frmMain.Width = inputVal
Input #iFileNo, inputVal: frmMain.Height = inputVal
It has to be read into a basic type, and then assigned to the object property.
There are a number of other options.
If you haven't perused it lately, you might want to look at the File I/O Tutorial.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
That example code worked perfectly, thank you very much. More importantly, I can see and understand what it is doing, so I've learned something new today. As for the draconian method of reading and writing the file, it's just something I picked up during my travels on Google, but, as you mentioned, I thought something was wrong when I couldn't get a read statement similar to it.
My only remaining question would be, in the read statement, as I want to apply the inputVal for txtForename to other textboxes, such as txtSurname, how would I extend the inputVal to other objects? (Edit: Of course - add subsequent lines for each object after the input statement, but before the next.
I did actually read through some of the File I/O Tutorial earlier, but I have to admit, a lot of it went over my head as I'm so out of practice and hadn't done much work with file I/O previously.
Thanks again, your time and effort is much appreciated.
Last edited by MarkB85; 03-20-2012 at 08:02 PM.
Reason: Code update with answer to question.
I did actually read through some of the File I/O Tutorial earlier, but I have to admit, a lot of it went over my head as I'm so out of practice and hadn't done much work with file I/O previously.
passel is right..
but sometimes you have to take baby steps to
play around with things and understand them,
so I made a small attachment to try and help
based on your original I/O code..
Quote:
I thought something was wrong when I couldn't get a read statement similar to it.
..it includes some file read code
that is sort of the equivalent of the level of write code you were using.
Some long color to color name info:
(this chart is one I keep bookmarked)
0 - Black, 0,0,0
255 - Red, 255, 0, 0
8388608 - Dark Blue, 0, 0, 128
65535 - Yellow, 255,255,0
12615935 - FFC080 - Cornbread, 255, 192, 128
I got these values using this VB6 long color conversion code
(included as part of a hidden button in my attachment):
Code:
Function FindRGB(Col As Long)
Dim strRGB As String
R = &HFF& And Col
G = (&HFF00& And Col) \ 256
B = (&HFF0000 And Col) \ 65536
FindRGB = "Red: " & R & "," & vbCrLf & _
"Blue: " & B & "," & vbCrLf & _
"Green: " & G
End Function
..and using rpgnewbie's "VB_RGB_to_Hex_ColorMixer.zip" attached to this post.
Also for others who may happen on this thread,
please remember normal hex is rgb (red, green blue)
but when dealing with VB6 control's ForeColor and BackColor properties
it's actually a little backwards hex - bgr (blue, green, red)
so rgb hex "FFC080" becomes &H0080C0FF& for lblForeName.ForeColor
Thanks for taking the time to create the example, I'll definitely get a look at it in the next few days and play around with it to see what it's doing.
I think I don't get your point, but you stated in the first lines you wanted to save option values. This is normally done by the registry or by an .ini file instead of any other type of file.
I think I don't get your point, but you stated in the first lines you wanted to save option values. This is normally done by the registry or by an .ini file instead of any other type of file.
You can use any file type, method, data layout or whatever you want.
No need to restrict yourself to INI files or the registry.
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET. subscribe