Results 1 to 7 of 7

Thread: Loading a file... i'll go into as much detail as possible

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    221

    Loading a file... i'll go into as much detail as possible

    Right, i've asked similar questions a few times and got different answers and it has completely confused me so i've deicded to put exactly what i want in as much detail as possible. So, here we go:

    I know how to create text files and am completely certain on that front. What i want to do is load a text file back into my program.

    What i want it to do is take the top line and put it in, let's say label1.caption. and then take the second line and put it in label2.caption and third line in label3.caption. how would i do this? if i need to put some nessacery text in the file which would be a piece of code or something then tell me what it is and what i need to put.

    Please give it as exactly what i need aswell, not a link to something which is a "variation" of what i want (which'll just confuse me and end in me asking again). thanks.

  2. #2
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Loading a file... i'll go into as much detail as possible

    if your labels are named label1, label2, label3 etc, then you can do this:
    VB Code:
    1. Dim N As Long, sLine As String, FF As Long
    2.     FF = FreeFile
    3.     Open "C:\file.txt" For Input As #FF
    4.         Do Until EOF(FF)
    5.             Line Input #FF, sLine
    6.             N = N + 1
    7.             Me.Controls("Label" & N).Caption = sLine
    8.         Loop
    9.     Close #FF

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    221

    Re: Loading a file... i'll go into as much detail as possible

    still not sure. so if i put in the file:


    test
    test2
    test3

    then label1.caption will be test, label2.caption will be test2 and label3.caption will be test3, is that right?

  4. #4

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    221

    Re: Loading a file... i'll go into as much detail as possible

    and what would i do if i wanted say, the first line to be label1.caption, second line to go to label4.caption, thrid line to go to text1.text and 4th line to go to command1.caption?
    (just chose some random things for this example)

  6. #6
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: Loading a file... i'll go into as much detail as possible

    The you would need to manually check the value of "N" (select case) and set the control accordingly

  7. #7
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Loading a file... i'll go into as much detail as possible

    Or add them to a collection and loop through the collection: i.e.
    VB Code:
    1. Private colControls As Collection
    2.  
    3. Private Sub Form_Load()
    4.     Set colControls = New Collection
    5.     With colControls
    6.         .Add Label1, Label1.Name
    7.         .Add Label4, Label4.Name
    8.         .Add Text1, Text1.Name
    9.         .Add Command1, Command1.Name
    10.     End With
    11. End Sub
    12.  
    13. Private Sub Form_Unload(Cancel As Integer)
    14.     Set colControls = Nothing
    15. End Sub
    16.  
    17. Private Sub Command1_Click()
    18.     Dim N As Long, sLine As String, FF As Long
    19.     FF = FreeFile
    20.     Open "C:\file.txt" For Input As #FF
    21.         Do Until EOF(FF)
    22.             Line Input #FF, sLine
    23.             N = N + 1
    24.             Select Case True
    25.                 Case TypeOf colControls(N) Is TextBox
    26.                     colControls(N).Text = sLine
    27.                 Case Else
    28.                     colControls(N).Caption = sLine
    29.             End Select
    30.         Loop
    31.     Close #FF
    32. End Sub

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