1

I'm trying to place text from a text file in a textbox, but the textbox remains blank after the code executes. How can I fix this?

Dim fileno1 As Integer = FreeFile()
FileOpen(fileno1, "C:\Users\main computer\Desktop\vb test\gyn-obs-D.txt", OpenMode.Input, OpenAccess.Read, OpenShare.Shared)
Dim y As Boolean = 0
Dim c = 0
TextBox1.Text = "1"
Do While Not EOF(fileno1)
    c += 1
    Dim txt As String = LineInput(fileno1)
    Debug.WriteLine(txt)
    Dim inputString As String = txt

    TextBox1.Text = txt
    If c = 40 Then
        y = 1
        Exit Do
    End If
    write1(inputString, y)
Loop
FileClose(fileno1)

edit: i added this class but still something wrong

' of course these next two are at top Imports System Imports System.IO

Class Test
    Public Shared Sub Main()
        Try
            ' Create an instance of StreamReader to read from a file.
            ' The using statement also closes the StreamReader.
            Using sr As New StreamReader("TestFile.txt")
                Dim line As String
                ' Read and display lines from the file until the end of
                ' the file is reached.
                Do
                    line = sr.ReadLine()
                    If Not (line Is Nothing) Then
                        Console.WriteLine(line)
                    End If
                         textbox1.text=line  
                Loop Until line Is Nothing
            End Using
        Catch e As Exception
            ' Let the user know what went wrong.
            Console.WriteLine("The file could not be read:")
            Console.WriteLine(e.Message)
        End Try
    End Sub
End Class
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
md nth
  • 119
  • 1
  • 1
  • 6

6 Answers6

3

How about

TextBox.Text = System.IO.File.ReadAllText("C:\Users\main computer\Desktop\vb test\gyn-obs-D.txt")

If that is too long

TextBox.Text = System.IO.File.ReadAllText("C:\Users\main computer\Desktop\vb test\gyn-obs-D.txt").Substring(0,1000)
Mark Lakata
  • 19,989
  • 5
  • 106
  • 123
0
TextBox1.Text = txt

This line will effectively errase what you had in the text box with contents of txt. I am assuming the 40th line of your input file is a blank line. That is way the textbox appears empty.

You should do something in the lines of:

TextBox1.Text = TextBox1.Text + txt + Environment.NewLine

Some pointers on your current code:

Hemario
  • 140
  • 9
  • If he is in fact using VB 6 rather than (which seems more likely), this answer is not going to be particularly helpful. There's no `Environment.NewLine` in VB 6, it's the `vbNewLine` constant. And there's no `Option Strict` either, although `Option Explicit` should be at the top of all source files. – Cody Gray - on strike Jul 10 '13 at 19:44
  • The original question was marked vb.net, but has been changed in the meantime. The fact remains that the contents of TextBox1.Text is being overwritten in each loop. `vbNewLine` instead of `Environment.Newline` and we're good to go. – Hemario Jul 10 '13 at 19:51
  • I'm not criticizing your answer, I know the question is/was unclear. Just adding some extra info if it is VB 6. – Cody Gray - on strike Jul 10 '13 at 19:51
  • its not vb6 for sure, its vb 2010,and i used book by wrox,and the 40 line is not empty – md nth Jul 10 '13 at 20:01
  • 1
    Time to throw that book away, @mdnth. It's well over 10 years out of date. It's teaching you how to write VB 6 code, which only works in VB 2010 for backwards compatibility reasons. You should *not* be learning VB 6 in the year 2013. Get yourself a new book on VB.NET, preferably version 2010 or 2012. – Cody Gray - on strike Jul 10 '13 at 20:04
  • You'll need to elaborate what it is exactly what you're trying to accomplish. – Hemario Jul 10 '13 at 20:09
0

Simply do this,

Private Sub Command1_Click()
Open "C:\Users\reserve.txt" For Input As #1
Dim filesize As Integer
filesize = LOF(1)
textbox1 = Input(filesize, #1)
Close #1
End Sub

Or,

Private Sub Command1_Click() 
Dim variable1 As String 
Open "C:\Users\reserve.txt" For Input As #1 
Input #1, variable1 
textbox1.Text = variable1 
Close #1 
End Sub

Or,see How to display the text file while clicking the button

Community
  • 1
  • 1
ridoy
  • 6,274
  • 2
  • 29
  • 60
0

Here's a simple way in VB.net:

    Try
        For Each s As String In System.IO.File.ReadAllLines("C:\Users\main _
                                      computer\Desktop\vb test\gyn-obs-D.txt")
            TextBox1.AppendText(s + vbNewLine)
        Next
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

This way if there are any changes to make or lines you don't want you have the option.

tinstaafl
  • 6,908
  • 2
  • 15
  • 22
-1

I figured it out, when I output data into textbox too fast , it'll not appear

Community
  • 1
  • 1
md nth
  • 119
  • 1
  • 1
  • 6
-1

'Easy Code.....................From Juman Dim b As String b = System.IO.File.ReadAllText("File Address Here")

            MessageBox.Show(b.ToString())
Juman
  • 1