CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2001
    Posts
    3

    Displaying Text inside a txtBox

    Displaying Text inside a txtBox
    Does anyone know how to do this? I have already declared a variable, and I just want to dump the contents of the variable (a txt file) into a text box -- and later parse this information.

    I've played around with debug.print, but is there a way to dump that data into a text box?

    Thanks




  2. #2
    Join Date
    Aug 2000
    Location
    KY
    Posts
    766

    Re: Displaying Text inside a txtBox

    text1.text = YourVariable


  3. #3
    Join Date
    Jul 2001
    Posts
    3

    Re: Displaying Text inside a txtBox

    I did that, however, it doesn't work.

    The way I have it setup is that there is a cmdButton -- when you click on it it brings up a file open dialog. right now when you double click on the file the text box displays the LOCATION of the file (ie: c:\temp\file.txt)

    I would like to figure out how to display the CONTENTS of the text file within that text box.

    Thanks


  4. #4
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Displaying Text inside a txtBox

    In order to dump the contents of the file into the textbox, you will need to read the records in the file and add them yourself. The CommonDIalog does not read the file it only passes you the path and filename of the user selected file.
    Here is code to do this. It's very simple and assumes no errors will take place.

    private Sub Command1_Click()
    ' set your TextBox for ScrollBars = Both
    ' set your TextBox for Multiline = true
    Dim Temp as string
    CommonDialog1.ShowOpen
    Open CommonDialog1.FileName for input as #1
    Text1.Text = "" ' clear out textBox
    Do Until EOF(1)
    Line input #1, Temp
    Text1.Text = Text1.Text & Temp & vbCrLf

    Loop
    End Sub




    John G

  5. #5
    Join Date
    Aug 2000
    Location
    KY
    Posts
    766

    Re: Displaying Text inside a txtBox

    oh, start a new project, add a common dialog box and and a text box then paste this code in. Then add a reference to the microsoft scripting runtime to your project from the project refereneces selection.


    public Function DisplayFile(fileName as string) as string
    Dim fso as FileSystemObject
    Dim ts as TextStream
    set fso = new FileSystemObject
    If fso.FileExists(fileName) then
    set ts = fso.OpenTextFile(fileName, ForReading)
    DisplayFile = ts.ReadAll
    End If


    End Function

    private Sub Form_Load()
    With CommonDialog1
    .ShowOpen
    If len(.fileName) > 0 then
    Text1 = DisplayFile(.fileName)
    End If
    End With

    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

Featured