【问题标题】:Read .txt file and display into different button读取 .txt 文件并显示到不同的按钮
【发布时间】:2015-10-12 11:52:32
【问题描述】:

我试图在我的按钮上显示一些文本,但我只能在 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

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

【问题讨论】:

    标签: vb.net visual-studio


    【解决方案1】:

    使用类似...

    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
    

    【讨论】:

    • 感谢您的回复。我先试试。
    【解决方案2】:
    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-09
      • 1970-01-01
      相关资源
      最近更新 更多