2015-10-12 56 views
0

我想顯示在我的按鈕一些文字,但我只可以在1個按鈕顯示的文本。我的按鍵是分開的像Button1.TextButton2.TextButton3.Text和我.txt文件只能顯示在Button1.Text。這是我迄今爲止所做的代碼。閱讀txt文件,並顯示爲不同的按鈕

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 

,我想我的系統能夠閱讀並顯示到每個按鈕。怎麼做?謝謝。

回答

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 
+0

謝謝你的回覆。我先嚐試一下。 – Emerald

+0

非常感謝。它的工作:) – Emerald

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