vb.net 每次点击按钮时,如何在visual basic 1行中从文本文件读取到文本框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17409376/
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
how to read from text file to textbox in visual basic 1 line every hit on button
提问by Samer Yousef
I have files type .txt (Text File) and it's have multiple line and i have these pictures
我有文件类型 .txt(文本文件),它有多行,我有这些图片
i want to make program generate fake information
我想让程序生成虚假信息
Mean: when some one hit generate button it's ready from text file and fill textbox in visual basic
意思是:当有人点击生成按钮时,它就可以从文本文件中准备好并在visual basic中填充文本框
every hit(press) on Generate Button make program generate new information from text files (.txt)
每次点击(按下)生成按钮使程序从文本文件(.txt)生成新信息
i tried a lot ways:
我尝试了很多方法:
Code:
代码:
Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText("C:\test.txt")
Code:
代码:
Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText("C:\test.txt", _
System.Text.Encoding.UTF32)
and this
和这个
Code:
代码:
Dim oFile as System****.File
Dim oRead as System****.StreamReader
oRead = oFile.OpenText(“C:\test.txt”)
and this
和这个
Code:
代码:
Dim FILE_NAME As String = "C:\Users\user\Desktop\test.txt"
Dim objReader As New System.I--O.StreamReader(FILE_NAME)
TextBox1.Text = objReader.ReadToEnd
Code:
代码:
' Form Load :
Dim text As String = MsgBox("text you want to make the form remember it.")
Or new Sub :
Code:
Private Sub Text
text
Code:
' Button Click :
Text()
Code:
代码:
Dim path As String = "THE FILE PATH" 'The file path
Dim reader As New IO.StreamReader(path)
Dim lineIndex As Integer = 2 ' The index of the line that you want to read
For i As Integer = 0 To lineIndex - 1
reader.ReadLine()
Next
TextBox1.Text = reader.ReadLine
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = ReadLineFromTxt("THE TXT FILE PATH", 0) '0 is the line index
End Sub
Public Shared Function ReadLineFromTxt(ByVal path As String, ByVal lineIndex As Integer) As String
Dim reader As New IO.StreamReader(path)
For I As Integer = 0 To lineIndex - 1
reader.ReadLine()
Next
Return reader.ReadLine()
End Function
End Class
These ways take from members in this fourm: http://www.mpgh.net/forum/33-visual-basic-programming/693165-help-how-can-i-generate-text-txt-file-textbox-when-button-click-2.html
这些方法取自本期成员:http://www.mpgh.net/forum/33-visual-basic-programming/693165-help-how-can-i-generate-text-txt-file-textbox-when -button-click-2.html
if are these ways working please tell me how to use it in best way
如果这些方法有效,请告诉我如何以最佳方式使用它
i have Visual studio 2012 and updated 1
我有 Visual Studio 2012 并更新了 1
With all due respect
恕我直言
回答by StingyHyman
Assuming you are reading from a file and displaying lines on the form, you can use these.
假设您正在读取文件并在表单上显示行,您可以使用这些。
If you have a large file (> 10MB), then you can use this pattern... (syntax from memory, please excuse mistype)
如果你有一个大文件(> 10MB),那么你可以使用这个模式......(内存中的语法,请原谅输入错误)
Public Class YourFormNameHere
Private _CurrentLine as Integer = 0
Private Sub btnClicked(sender, e) 'or enter pressed - This is YOUR keypress event handler.
Using Dim sr as New StreamReader(filePath)
Dim _curIndex as Integer = 0
While (sr.EndOfFile == false)
Dim _line as String = sr.ReadLine()
If (_curIndex = _CurrentLine)
txtLineDisplay.Text = _line
Break
End If
curIndex += 1
End While
End Using
End Sub
End Class
If you have a smaller file, then use this pattern.
如果您的文件较小,请使用此模式。
Public Class YourFormNameHere
Private _Lines as String()
Private _CurrentLine as Integer = 0
Private Sub formLoad(sender, e) 'one-time load event - This is YOUR form load event
_Lines = File.ReadAllLines(filePath)
End Sub
Private Sub btnClicked(sender, e) 'or enter pressed - This is YOUR keypress event handler.
txtLineDisplay.Text = _Lines[_CurrentLine]
_CurrentLine += 1
End Sub
End Class

