samedi 28 mars 2015

How to create variables dynamically based on a string array?

I have a string array Globals.stringArray that populates a list of controls based on the amount of entries in the array. I want to track how many times the control is used (eg. a button clicked) with an integer that I will use and display at a later time.


I assume that a dynamically created global variable is needed depending on how many controls are created (based on how many items in the string array).


Secondly, how would I do this and how would I access these variables afterwards?


Here is what I have:



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


I am assuming that I can add the dynamicVariables() into where my buttons are created to have it run in conjunction with that and map the variables to the buttons?


EDIT: (with suggestions)



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


EDIT 2: (with suggestions)



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

Aucun commentaire:

Enregistrer un commentaire