Vb.net 读取.txt文件并显示到不同的按钮中
我试图在我的按钮上显示一些文本,但我只能在1个按钮上显示这些文本。我的按钮像Button1.Text、Button2.Text和Button3.Text一样分开,我的.txt文件只能以Button1.Text显示。这是我到目前为止所做的代码Vb.net 读取.txt文件并显示到不同的按钮中,vb.net,visual-studio,Vb.net,Visual Studio,我试图在我的按钮上显示一些文本,但我只能在1个按钮上显示这些文本。我的按钮像Button1.Text、Button2.Text和Button3.Text一样分开,我的.txt文件只能以Button1.Text显示。这是我到目前为止所做的代码 Private Sub FormMenu_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim R As New IO.StreamReader("Tes
Private Sub FormMenu_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim R As New IO.StreamReader("TestFile.txt")
Button1.Text = R.ReadToEnd()
R.Close()
End Sub
在我的.txt文件中
First Button
Second Button
Third Button
我希望我的系统能够读取它们并显示在每个按钮上。怎么做?谢谢。使用类似于
Private Sub FormMenu_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim lines() As String = System.IO.File.ReadAllLines("TestFile.txt")
For i As Integer = 1 To lines.Length
Dim ctl As Control = Me.Controls.Find("Button" & i, True).FirstOrDefault
If Not IsNothing(ctl) Then
ctl.Text = lines(i - 1)
End If
Next
End Sub
谢谢你的回复。我先试试。
Private Sub FormMenu_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim R As New IO.StreamReader("TestFile.txt")
Dim words As String() = R.ReadToEnd().Split(New String() {Environment.NewLine})
Button1.Text = words(0)
Button2.Text = words(1)
Button3.Text = words(2)
R.Close()
End Sub