Results 1 to 15 of 15

Thread: Read Text From .txt

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2014
    Posts
    30

    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

  2. #2
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    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)

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2014
    Posts
    30

    Re: Read Text From .txt

    I havnt got an array.

    How else could I do this? Even if its line by line?

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Read Text From .txt

    Quote Originally Posted by PedroC1999 View Post
    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
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5
    Fanatic Member
    Join Date
    Jan 2013
    Posts
    759

    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.

  6. #6
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    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

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Dec 2014
    Posts
    30

    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

  8. #8
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    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.

  9. #9
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    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.....

  10. #10
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    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

  11. #11
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    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)))

  12. #12
    Fanatic Member
    Join Date
    Jan 2013
    Posts
    759

    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.

  13. #13
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Read Text From .txt

    WierdlyThisLabelIsNamed
    Yoda what say.

  14. #14
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    Re: Read Text From .txt

    Quote Originally Posted by Phill.W View Post
    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

  15. #15
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    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
  •  



Click Here to Expand Forum to Full Width