我想创建一个新按钮,其中我在Globals.stringItems
字符串数组中的每个项目都显示另一个表单上的文本,该数组通过.csv文本文件填充并显示为dataviewgrid。
这就是我所拥有的:
Public Class Globals
Public Shared stringItems As String()
End Class
Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(OpenFileDialog1.FileName)
MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
MyReader.Delimiters = New String() {", "}
Dim currentRow As String()
'Loop through all of the fields in the file.
'If any lines are corrupt, report an error and continue parsing.
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
DataGridView1.Rows.Add(currentRow)
Globals.stringItems = currentRow
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Line " & ex.Message & " is invalid. Skipping")
End Try
End While
End Using
End If
Public Function AddNewButton() As System.Windows.Forms.Button
Dim button As New System.Windows.Forms.Button()
For Each item As String In Globals.stringItems
Me.Controls.Add(button)
button.Text = ?????????
Next
Return button
End Function
我甚至关闭了吗?我错过了什么?我似乎无法弄清楚这一点。
答案 0 :(得分:0)
由于stringItems
被声明为Public Shared
,因此它在所有函数中被全局访问。所以你不需要像Globals.stringItems
那样指定。并且你的AddNewButton()
函数缺少一些逻辑。
我从你给出的片段中观察到了这些;
对于这种情况,您可以编写如下代码:
Public Sub AddNewButton()
Dim button As New System.Windows.Forms.Button()
Dim buttonTop As Integer = 100
For Each item As String In stringItems
Dim Location As New Point(100, (buttonTop + 20))
button.Location = Location
button.Text = item
button.Width=150
Me.Controls.Add(button)
buttonTop += 20
Next
End Function
如果stringItems
包含7个元素,则会创建7个按钮