将文件中的文本加载到文本框中 [英] Loading text from a file into a textbox

查看:41
本文介绍了将文件中的文本加载到文本框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将文本文件中的文本放入文本框中,但在代码执行后该文本框仍为空白.我该如何解决这个问题?

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

' 当然接下来这两个是最上面的进口系统导入 System.IO

' 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

推荐答案

怎么样

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)

这篇关于将文件中的文本加载到文本框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆