Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > File I/O and Registry > Reading from lines in a text file


Reply
 
Thread Tools Display Modes
  #1  
Old 03-20-2012, 03:17 PM
MarkB85 MarkB85 is offline
Newcomer
 
Join Date: Mar 2012
Posts: 6
Default Reading from lines in a text file


Hi all,

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.

Many thanks for any assistance.
Reply With Quote
  #2  
Old 03-20-2012, 06:25 PM
passel's Avatar
passel passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,862
Default

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.
Reply With Quote
  #3  
Old 03-20-2012, 06:59 PM
MarkB85 MarkB85 is offline
Newcomer
 
Join Date: Mar 2012
Posts: 6
Default

Hi passel,

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.

Code:
Input #iFileNo, inputVal: frmMain.BackColor = inputVal
Input #iFileNo, inputVal: frmMain.txtForename.BackColor = inputVal
    frmMain.txtMiddleInitial.BackColor = inputVal
    frmMain.txtSurname.BackColor = inputVal
Input #iFileNo, inputVal: frmMain.txtForename.ForeColor = inputVal
etc. Works perfectly.

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.
Reply With Quote
  #4  
Old 03-20-2012, 07:57 PM
hDC_0 hDC_0 is offline
Contributor

* Expert *
 
Join Date: Feb 2004
Posts: 537
Default Learning in baby steps..

Quote:
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 the "Cornbread" name as the "MarthaSteward" color from here:
http://www.colourlovers.com/color/FFC080/Blankly

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

edit: Besides the GavinO's File I/O tutorial, here's some other Code Library threads to take a look at:
MikeJ's Common Dialog Tutorial (includes Color Dialog)
OnErr0r's "Opening and Positioning a Color Common Dialog"
Attached Images
File Type: jpg MarkB85_Colors_from_File.JPG (54.7 KB, 11 views)
Attached Files
File Type: zip MarkB85_ColorsLoaded_Froms_Prefs_Text_File.zip (3.3 KB, 7 views)

Last edited by hDC_0; 03-20-2012 at 08:27 PM.
Reply With Quote
  #5  
Old 03-21-2012, 04:43 PM
MarkB85 MarkB85 is offline
Newcomer
 
Join Date: Mar 2012
Posts: 6
Default

Hi hDC_0,

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.
Reply With Quote
  #6  
Old 04-04-2012, 12:16 PM
vbmani vbmani is offline
Newcomer
 
Join Date: Oct 2011
Posts: 14
Default

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.

INI FILE
Saving values: WritePrivateProfileString
Retrieving values: GetPrivateProfileString

Declarations:
Private Declare Function GetPrivateProfileSection& Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName$, ByVal lpReturnedString$, ByVal nSize&, ByVal lpFileName$)
Private Declare Function GetPrivateProfileInt& Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName$, ByVal lpKeyName$, ByVal nDefault&, ByVal lpFileName$)
Private Declare Function GetPrivateProfileString& Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName$, ByVal lpKeyName As Any, ByVal lpDefault$, ByVal lpReturnedString$, ByVal nSize&, ByVal lpFileName$)
Private Declare Function WritePrivateProfileSection& Lib "kernel32.dll" Alias "WritePrivateProfileSectionA" (ByVal lpAppName$, ByVal lpString$, ByVal lpFileName$)
Private Declare Function WritePrivateProfileString& Lib "kernel32.dll" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName$, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName$)

REGISTRY
Saving values: SaveSetting
Retrieving values: GetSetting

Or with API's:
Declare Function RegCloseKey& Lib "advapi32" (ByVal hKey&)
Declare Function RegCreateKey& Lib "advapi32" Alias "RegCreateKeyA" (ByVal hKey&, ByVal lpSubKey$, phkResult&)
Declare Function RegOpenKey& Lib "advapi32" Alias "RegOpenKeyA" (ByVal hKey&, ByVal lpSubKey$, phkResult&)
Private Declare Function RegDeleteValue& Lib "advapi32" Alias "RegDeleteValueA" (ByVal hKey&, ByVal lpValueName$)
Private Declare Function RegQueryValueEx& Lib "advapi32" Alias "RegQueryValueExA" (ByVal hKey&, ByVal lp2valueName$, ByVal lpReserved&, lpType&, lpData As Any, lpcbData&)
Private Declare Function RegSetValueEx& Lib "advapi32" Alias "RegSetValueExA" (ByVal hKey&, ByVal lp2valueName$, ByVal Reserved&, ByVal dwType&, lpData As Any, ByVal cbData&)
Reply With Quote
  #7  
Old 04-04-2012, 03:17 PM
Flyguy's Avatar
Flyguy Flyguy is offline
Lost Soul

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 19,039
Default

Quote:
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.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
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
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->