-
Feb 7th, 2015, 05:00 AM
#1
Thread Starter
Junior Member
Read Text From .txt
Im trying to get my VB6 program to 'read' a .txt file, which has multiples lines, each with a integer.
I want the program to set the Integer as a value (In this case, writing to a specific label box) then do the same with the below number, to another box etc)
The .txt has 11 lines with 11 integers, and I have 11 label boxes.
How would I do this? Thank you
-
Feb 7th, 2015, 08:13 AM
#2
Re: Read Text From .txt
The following example assumes your Label controls are part of a control array with indexes from 0 to 10:
Code:
Private Sub Command1_Click()
Dim i As Integer, FN As Integer, sLine As String
FN = FreeFile
On Error GoTo 1
Open App.Path & "\File.txt" For Input As FN
For i = 0 To 10
If Not EOF(FN) Then
Line Input #FN, sLine
Label(i).Caption = sLine
Else
Exit For
End If
Next
1 Close FN
End Sub
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Feb 10th, 2015, 12:33 PM
#3
Thread Starter
Junior Member
Re: Read Text From .txt
I havnt got an array.
How else could I do this? Even if its line by line?
-
Feb 10th, 2015, 01:19 PM
#4
Re: Read Text From .txt
Originally Posted by PedroC1999
I havnt got an array.
Suggestion: Create an array of textboxes to make this easy for you. Then from sample code above, replace Label(...).Caption with TxtWhatever(...).Text
If absolutely needed, and your textboxes are named the same with a different number suffix, then from the sample code you can replace this line: Label(i).Caption with this one:
Me.Controls("txtWhatever" & CStr(i + 1)).Text = sLine
Assumption is that txtWhatever suffix starts at 1. Else modify the (i+1) part as needed
-
Feb 11th, 2015, 07:38 AM
#5
Re: Read Text From .txt
Control Arrays are simply invaluable for things like this. Trying to do something like this without them is painfully repetitive.
You say you "haven't got an array" - is anything stopping you from using one? (Is this an assignment?).
Regards, Phill W.
-
Feb 11th, 2015, 09:52 AM
#6
Re: Read Text From .txt
Here's how it would (could) look like withOUT using an ARRAY of textboxes (or labels) . See how this is NOT the way to go?
Code:
Dim myLine, selfile As String
selfile = App.Path + "\yourFile.txt"
Open selfile For Input As #1 ' Open file for input.
Line Input #1, myLine ' Read first line of data.
Text1.Text = myLine
Line Input #1, myLine ' Read next line of data.
Text2.Text = myLine
Line Input #1, myLine ' Read next line of data.
Text3.Text = myLine
Line Input #1, myLine ' Read next line of data.
Text4.Text = myLine
Line Input #1, myLine ' Read next line of data.
Text5.Text = myLine
Line Input #1, myLine ' Read next line of data.
Text6.Text = myLine
Line Input #1, myLine ' Read next line of data.
Text7.Text = myLine
Line Input #1, myLine ' Read next line of data.
Text8.Text = myLine
Line Input #1, myLine ' Read next line of data.
Text9.Text = myLine
Line Input #1, myLine ' Read next line of data.
Text10.Text = myLine
Line Input #1, myLine ' Read next line of data.
Text11.Text = myLine
Close #1
-
Feb 11th, 2015, 02:45 PM
#7
Thread Starter
Junior Member
Re: Read Text From .txt
For me an array is not suitable (I dont think) since almost all labels are named differently and quite significantly different.
Not an assignment, personal mini project
-
Feb 11th, 2015, 03:10 PM
#8
Re: Read Text From .txt
So you need to read a line and assign the caption to your first label then read a line and assign the caption of your next label and so on.
An array makes things a lot easier.
-
Feb 11th, 2015, 03:12 PM
#9
Re: Read Text From .txt
....Like I showed you (I used a textbox in lieu of a label because I didn't know what a "label box" was in your lingo.....
Of course, the number of lines in the file will have to be equal to, or less than, the number of "label boxes" you have, otherwise you will error out.....
-
Feb 11th, 2015, 03:23 PM
#10
Re: Read Text From .txt
To create a control array at design-time.
1) Place one label control on the form. Give it a generic name. Say... lblDigit.
2) Right mouse on the label control and select Copy.
3) Click on the form right mouse and select Paste.
4) VB6 will show a dialog asking if you want to create a control array of the label.
Select Yes.
5) The original label will now have and index property value of 0.
The copied label will have an index property value of 1.
6) Repeat the paste function as many times as you like.
new copies of the label will have increasing index numbers assigned.
They will all have the same name but with different indexes.
You can access each label in the array by using it's index as an offset.
Code:
lblDigit(0).Caption = 10
lblDigit(1).Caption = 22
'Etc...
Because they are an array you can use a loop to fill them.
If you create an event for the original label control (lblDigit(0)) the resulting Sub-Routine will have an index parameter.
Code:
Private Sub lblDigit_Click(Index As Integer)
Msgbox lblDigit(index).Caption
'If the user clicked on lblDigit(0) then "10" will be displayed.
'If the user clicked on lblDigit(1) then "22" will be displayed.
'Etc...
End Sub
Last edited by Gruff; Feb 11th, 2015 at 03:27 PM.
Burn the land and boil the sea
You can't take the sky from me
~T
-
Feb 11th, 2015, 03:28 PM
#11
Re: Read Text From .txt
What do you mean by:
since almost all labels are named differently and quite significantly different.
A label is a label. The CAPTION is what changes, not the control itself. If they have different NAMES, I'd advise you to change that, and call them all ONE name, but create an array of them. You may have to recode some other stuff to reflect the name changes (for example, it might have been lblTotal and lblSum, but could now be lblMath(0) and lblMath(1), all the way up to 11 labels (lblMath(10) would be the last (11th) label because you start an array with 0 (normally)))
-
Feb 12th, 2015, 03:48 AM
#12
Re: Read Text From .txt
Borrowing from .Net thinking - don't shoot me - you could still use a Control Array or, at least, an array of Controls.
You can create an array of Type Label and put the differently named Labels into it, the loop through the array that you've created - just remember that you're working with Objects (Controls) here, so you need to use the Set keyword to work with the object references:
Code:
Dim LabelArr( 5 ) as Label
Set LabelArr( 0 ) = Label1
Set LabelArr( 1 ) = SomeOtherLabel
Set LabelArr( 2 ) = WierdlyThisLabelIsNamed
Dim iIndex as Long : iIndex = 0
Do While( Not( EOF( #iFile ) ) )
Line Input #iFile, sLine
LabelArr( iIndex ) = sLine
iIndex = iIndex + 1
Loop
Regards, Phill W.
-
Feb 12th, 2015, 08:00 AM
#13
-
Feb 12th, 2015, 10:01 AM
#14
Re: Read Text From .txt
Originally Posted by Phill.W
Control Arrays are simply invaluable for things like this. Trying to do something like this without them is painfully repetitive.
You say you "haven't got an array" - is anything stopping you from using one? (Is this an assignment?).
Regards, Phill W.
Don't forget that VBA in Office doesn't support Control-Arrays.
Even if he says "his VB6-Program", nowadays you never know if it's Visual Studio or if it's VBA/Office he uses
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Feb 12th, 2015, 10:08 AM
#15
Re: Read Text From .txt
Based upon OP's history of posts, I BELIEVE he IS using VB6 (but in one post indicated he may try to rewrite that code in that Thread in .Net). So, PROBABLY he is using VB6.0....but, it's a good point to point out.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|