-1

我有文件类型 .txt(文本文件),它有多行,我有这些图片

我想让程序生成虚假信息

意思是:当某个点击生成按钮时,它已从文本文件准备好并在 Visual Basic 中填充文本框

生成按钮上的每次点击(按下)都会使程序从文本文件(.txt)中生成新信息

我尝试了很多方法:

代码:

Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText("C:\test.txt")

代码:

Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText("C:\test.txt", _
   System.Text.Encoding.UTF32)

和这个

代码:

 Dim oFile as System****.File
Dim oRead as System****.StreamReader
oRead = oFile.OpenText(“C:\test.txt”)

和这个

代码:

 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

代码:

' 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()

代码:

    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

这些方法来自这四个成员: http ://www.mpgh.net/forum/33-visual-basic-programming/693165-help-how-can-i-generate-text-txt-file-textbox-when -button-click-2.html

如果这些方法有效,请告诉我如何以最佳方式使用它

我有 Visual Studio 2012 并更新了 1

恕我直言

4

1 回答 1

1

假设您正在读取文件并在表单上显示行,您可以使用这些。

如果你有一个大文件(> 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

如果您的文件较小,请使用此模式。

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
于 2013-07-01T16:44:25.700 回答