vb.net 将文件中的文本加载到文本框中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17579333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:13:40  来源:igfitidea点击:

Loading text from a file into a textbox

vb.net

提问by md nth

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

' 当然,接下来的两个是最上面的 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

回答by Mark Lakata

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)

回答by md nth

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

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

回答by Hemario

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.

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

You should do something in the lines of:

你应该做一些事情:

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


Some pointers on your current code:

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

回答by ridoy

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

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

回答by tinstaafl

Here's a simple way in VB.net:

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

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

回答by Juman

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

'Easy Code.....................来自 Juman Dim b As String b = System.IO.File.ReadAllText("此处的文件地址")

            MessageBox.Show(b.ToString())