CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2006
    Posts
    23

    [RESOLVED] textfile,...??

    Is there a way for me to get specific information from a text file and insert them into specific textboxes? i just need a very simple way since am a newbie at this. Any help would be greatly appreciated. Thanks in advanced
    Last edited by tracer_god; June 30th, 2006 at 06:41 PM.

  2. #2
    Join Date
    Mar 2005
    Posts
    226

    Re: textfile,...??

    You will have to read it. After you read it, you can parse the string. One easy way to do it is use the Instr function.

  3. #3
    Join Date
    May 2006
    Posts
    23

    Question Re: textfile,...??

    Quote Originally Posted by RoyK
    You will have to read it. After you read it, you can parse the string. One easy way to do it is use the Instr function.
    How do i read it?

  4. #4
    Join Date
    Feb 2005
    Location
    Kolkata, India
    Posts
    430

    Re: textfile,...??

    You can use the built in file manupulation function of VB.

    Example:

    Dim TextLine
    Open "TESTFILE" For Input As #1 ' Open file.
    Do While Not EOF(1) ' Loop until end of file.
    Line Input #1, TextLine ' Read line into variable.
    Debug.Print TextLine ' Print to the Immediate window.
    Loop
    Close #1 ' Close file

    Hope you will be able to find your way.

    Regards
    Amarjit

  5. #5
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: textfile,...??

    Or, check this out. It splits on lines, but you coulld just as easily split on spaces to get words.
    Code:
    Option Explicit
    
    Private Sub Form_Load()
      Dim x As Integer, st As String
      Dim ff As Integer
      Dim strBuff As String
      Dim str() As String
      ff = FreeFile
      Open App.Path & "\to do.txt" For Input As #ff
        strBuff = Input(LOF(ff), ff)
      Close #ff
      ' ----------------- two ways to skin a cat --------------
     ' MsgBox "Lines = " & Len(strBuff) - Len(Replace(strBuff, vbCrLf, "x")) + 1
      ' -------------------------------------------------------
      str() = Split(strBuff, vbCrLf)
      MsgBox "There are " & UBound(str) + 1 & " lines in the file"
      For x = 0 To UBound(str)
        st = st & str(x) & vbCrLf & vbCrLf
      Next x
      MsgBox st
    End Sub
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  6. #6
    Join Date
    Jun 2006
    Posts
    14

    Re: textfile,...??

    hi
    this code will read the text file and disply it in a text box (txtLoadedText)

    FilePath = "c:\123.txt"
    Open FilePath For Binary As #1
    txtLoadedText = Input(LOF(1), 1)
    Close #1

  7. #7
    Join Date
    May 2006
    Posts
    23

    Talking Re: textfile,...??

    Quote Originally Posted by dglienna
    Or, check this out. It splits on lines, but you coulld just as easily split on spaces to get words.
    Code:
    Option Explicit
    
    Private Sub Form_Load()
      Dim x As Integer, st As String
      Dim ff As Integer
      Dim strBuff As String
      Dim str() As String
      ff = FreeFile
      Open App.Path & "\to do.txt" For Input As #ff
        strBuff = Input(LOF(ff), ff)
      Close #ff
      ' ----------------- two ways to skin a cat --------------
     ' MsgBox "Lines = " & Len(strBuff) - Len(Replace(strBuff, vbCrLf, "x")) + 1
      ' -------------------------------------------------------
      str() = Split(strBuff, vbCrLf)
      MsgBox "There are " & UBound(str) + 1 & " lines in the file"
      For x = 0 To UBound(str)
        st = st & str(x) & vbCrLf & vbCrLf
      Next x
      MsgBox st
    End Sub
    YES!!! Just what i needed! Works Perfect! Thanks!!!!

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

Featured