我在Visual Studio 2017中使用Visual Basic。我正在尝试从文本文件创建按钮。如果文本文件包含像 测试1 TEST2 TEST3 它应该使用这些名称创建3个按钮。
Dim path As String = "c:\words.txt"
Dim readText() As String = File.ReadAllLines(path)
Dim s As String
Dim button As New System.Windows.Forms.Button()
Dim buttonTop As Integer = 100
For Each s In readText
Dim Location As New Point(100, (buttonTop + 20))
button.Location = Location
button.Text = s
button.Width = 100
Me.Controls.Add(button)
buttonTop += 20
button.Visible = True
Next
此代码创建按钮但不保存。随着循环的进行,它会创建它们并从表单中消失,只留下循环结束时的最后一个。 任何指导方针将不胜感激。谢谢。
答案 0 :(得分:1)
正确答案。感谢
Dim path As String = "c:\words.txt"
Dim readText() As String = File.ReadAllLines(path)
Dim s As String
Dim buttonTop As Integer = 20
For Each s In readText
Dim button As New System.Windows.Forms.Button()
Dim Location As New Point(100, (buttonTop + 30))
button.Location = Location
Button.Text = s
button.Width = 100
button.Height = 30
Me.Controls.Add(Button)
buttonTop += 30
button.Visible = True
Next