如何基于字符串数组动态创建变量?

时间:2015-03-26 23:13:13

标签: arrays vb.net winforms variables

我有一个字符串数组Globals.stringArray,它根据数组中的条目数填充控件列表。我想跟踪控件的使用次数(例如点击一个按钮),我将使用一个整数并在以后显示。

我假设需要动态创建的全局变量,具体取决于创建的控件数量(基于字符串数组中的项目数)。

其次,我将如何做到这一点以及之后如何访问这些变量?

这就是我所拥有的:

Public Shared Sub dynamicVariables()
    Dim variables As New Dictionary(Of String, Integer)()
    variables("MyDynamicVariable") = Globals.stringArray.Length
End Sub

我假设我可以将dynamicVariables()添加到创建按钮的位置,以使其与该按钮一起运行并将变量映射到按钮?

编辑:(有建议)

Public Class Form2

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    AddNewButton()

End Sub

Public Sub AddNewButton()

    Dim buttonTop As Integer = 100

    For Each item As String In Globals.candidates
        Dim btn As New System.Windows.Forms.Button()
        Dim Location As New Point(100, (buttonTop + 20))
        btn.Location = Location
        btn.Text = item
        btn.Width = 150
        AddHandler btn.Click, AddressOf Me.buttonClick
        Me.Controls.Add(btn)
        buttonTop += 20
    Next

End Sub

Private Sub buttonClick(sender As Object, e As EventArgs)

    Dim btn As Button = DirectCast(sender, System.Windows.Forms.Button)
    Dim result As Integer = MessageBox.Show(String.Format("Did you select {0} ?", btn.Text), "Confirmation", MessageBoxButtons.YesNo)
    If result = DialogResult.Yes Then
        Dim usage As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)

        If usage.ContainsKey("myButtonName") Then
            usage("myButtonName") += 1
        Else
            usage.Add("myButtonName", 1)
        End If
    Else
        MessageBox.Show("No pressed")
    End If

End Sub

End Class

编辑2 :(有建议)

Public Class Form2

    Private Sub From2_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        AddNewButton()

    End Sub

    Public Sub AddNewButton()

        Dim buttonTop As Integer = 100

        For Each item As String In Globals.candidates
            Dim btn As New System.Windows.Forms.Button()
            Dim Location As New Point(100, (buttonTop + 20))
            btn.Location = Location
            btn.Text = item
            btn.Width = 150
            AddHandler btn.Click, AddressOf Me.buttonClick
            AddHandler btn.Click, AddressOf Me.ClickCounter
            Me.Controls.Add(btn)
            buttonTop += 20
        Next

    End Sub

    Private Sub buttonClick(sender As Object, e As EventArgs)

        Dim btn As Button = DirectCast(sender, System.Windows.Forms.Button)

        Dim result As Integer = MessageBox.Show(String.Format("Did you select {0} ?", btn.Text), "Confirmation", MessageBoxButtons.YesNo)

        If result = DialogResult.Yes Then

            'Yes clicked. Add 1 to counter and show next screen. ???

        Else

            'No clicked. Return to screen to make proper selection.

        End If

    End Sub

    Private Sub ClickCounter(sender As Object, e As EventArgs)

        Dim btn As Button = TryCast(sender, Button)

        Dim usage As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)

        If btn Is Nothing Then Return

        If Not usage.ContainsKey(btn.Name) Then

            usage.Add(btn.Name, 0)
            usage(btn.Name) += 1

        End If


    End Sub

End Class

2 个答案:

答案 0 :(得分:0)

对于您的控件允许的每个操作,您必须在计数中添加1。因此,在具有共享变量的模块中创建全局变量。字典是正确的方法,但你不必尝试给它一个长度,因为它是可变的。我的VB有点生疏,但你应该得到要点。

'In your button click event

Dim usage As Dictionary(string, integer) = new Dictionary(string, Integer)

If usage.ContainsKey("myButtonName") then
    usage("myButtonName") += 1
Else
    usage.add("myButtonName", 1)
end if

您的控件的名称将是键,计数将是值。

答案 1 :(得分:0)

我直接将它作为Button控件的属性,而不是试图在某个地方跟踪全局变量。

为此,我将从Button继承一个新控件,并添加一个名为TimesClicked的属性。然后我将添加一个事件处理程序(记住:事件可以有多个处理程序)到我的新控件类型,每次按下按钮时都会更新该属性。现在我可以使用这个新控件替换所有现有按钮。

如果这太麻烦了,我仍然会建立事件,即事件可以有多个处理程序,以避免使用样板代码来处理常规按钮响应代码:

Private Sub ClickCounter(sender As Object, e As EventArgs)
    Dim btn As Button = TryCast(sender, Button)
    If btn Is Nothing Then Return

    If Not usage.ContainsKey(btn.Name) Then usage.Add(btn.Name, 0)
    usage(btn.Name) += 1
End Sub

无论哪种方式,您的AddButton()方法都是这样的:

Public Sub AddNewButton()

    Dim buttonTop As Integer = 100

    For Each item As String In Globals.candidates
        Dim btn As New System.Windows.Forms.Button()
        Dim Location As New Point(100, (buttonTop + 20))
        btn.Location = Location
        btn.Text = item
        btn.Width = 150
        AddHandler btn.Clikc, AddressOf Me.ClickCounter
        AddHandler btn.Click, AddressOf Me.buttonClick
        Me.Controls.Add(btn)
        buttonTop += 20
    Next

End Sub

这也将允许您扩展ClickCounter方法以接受和计数使用其他类型的控件和事件,而无需返回编辑更改按钮。