阅读.txt文件并显示到不同的按钮

时间:2015-10-12 02:54:49

标签: vb.net visual-studio

我试图在我的按钮上显示一些文字,但我只能在1个按钮上显示这些文字。我的按钮分为Button1.TextButton2.TextButton3.Text,我的.txt文件只能显示在Button1.Text中。这是我到目前为止所做的代码。

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

我希望我的系统能够读取它们并显示到每个按钮中。怎么做?谢谢。

2 个答案:

答案 0 :(得分:0)

使用像...这样的东西。

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

答案 1 :(得分:0)

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