1

我正在尝试将文本文件中的文本放入文本框中,但代码执行后文本框仍为空白。我怎样才能解决这个问题?

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)

编辑:我添加了这个类,但仍然有问题

' 当然接下来的两个在顶部 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
4

6 回答 6

3

怎么样

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

如果太长

TextBox.Text = System.IO.File.ReadAllText("C:\Users\main computer\Desktop\vb test\gyn-obs-D.txt").Substring(0,1000)
于 2013-07-11T05:01:54.727 回答
0

只需这样做,

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

或者

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

或者,请参阅如何在单击按钮时显示文本文件

于 2013-07-10T19:48:00.987 回答
0
TextBox1.Text = txt

此行将有效地擦除您在文本框中的内容,其内容为txt. 我假设您的输入文件的第 40 行是一个空行。这就是文本框显示为空的方式。

你应该做一些事情:

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

关于您当前代码的一些指示:

于 2013-07-10T19:41:18.890 回答
0

这是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

这样,如果有任何更改或您不希望的线路,您可以选择。

于 2013-07-11T04:53:46.343 回答
-1

我想通了,当我将数据输出到文本框太快时,它不会出现

于 2013-07-29T16:47:00.000 回答
-1

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

            MessageBox.Show(b.ToString())
于 2017-03-14T08:42:48.577 回答