我正在尝试将文本文本中的文本放在文本框中,但代码执行后文本框仍为空白。我该如何解决这个问题?
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.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
答案 0 :(得分:1)
怎么样
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)
答案 1 :(得分:0)
TextBox1.Text = txt
此行有效地会删除文本框中包含txt
内容的内容。
我假设输入文件的第40行是一个空行。这就是文本框显示为空的方式。
你应该做的事情是:
TextBox1.Text = TextBox1.Text + txt + Environment.NewLine
关于当前代码的一些指示:
检查Option Strict。它可以在项目的属性窗口中启用,并启用更高的质量生成的代码。
像EOF
这样的函数似乎就像移植到.NET的旧VB函数一样。查看Using the System.IO namespace to read text from a file
答案 2 :(得分: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
答案 3 :(得分: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
这样一来,如果你想要的make或行有任何改变,你可以选择。
答案 4 :(得分:0)
我把它想象出来,当我将数据快速输出到文本框中时,它不会出现
答案 5 :(得分:-1)
'简易代码.....................来自Juman Dim b As String b = System.IO.File.ReadAllText(" File Address Here")
MessageBox.Show(b.ToString())