如何制作单选按钮单击事件将项目添加到字典中

时间:2015-01-19 16:41:07

标签: vb.net-2010

我有一个表单,其中包含几个在运行时创建的单选按钮。单选按钮的数量将根据数据库中的记录数量而变化。 我可以从click事件中获取值,但是,无法将另一个项添加到字典中。下面的代码仅显示当前点击事件的信息,并且不会为第二,第三等单击事件添加其他项目。

Public Sub RadClick(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim rb As RadioButton
    Dim myDictionary As New Dictionary(Of Integer, String)
    Dim x As Integer = 0

    rb = DirectCast(sender, RadioButton)
    If rb.Checked = True Then
        x = x + 1
        myDictionary.Add(x, rb.Name)
    End If

    For Each pair As KeyValuePair(Of Integer, String) In myDictionary
        MsgBox(pair.Key & " - " & pair.Value)
    Next

End Sub

1 个答案:

答案 0 :(得分:1)

感谢您的回复。 我最终根据您的建议使用列表来使用它。

Public Sub ComboClick(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim counter As Integer = 0
    Dim a_cb As ComboBox
    a_cb = DirectCast(sender, ComboBox)
    Dim x = a_cb.Name
    Dim h As Integer = 0
    'Dim pair As KeyValuePair(Of String, String)
    btnAll.Enabled = False
    ListID.Clear()
    Try
        Select Case a_cb.SelectedItem
            Case Is = "P"
                myDictionary.Add(x, a_cb.SelectedItem)
                a_cb.Enabled = False
            Case Is = "A"
                myDictionary.Add(x, a_cb.SelectedItem)
                a_cb.Enabled = False
            Case Is = "L"
                myDictionary.Add(x, a_cb.SelectedItem)
                a_cb.Enabled = False
        End Select
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
    For Each pair As KeyValuePair(Of String, String) In myDictionary
        If pair.Value = "P" Then ListID.Add(pair.Value)
        If pair.Value = "A" Then ListID.Add(pair.Value)
        If pair.Value = "L" Then ListID.Add(pair.Value)
        'MsgBox(pair.Key & " - " & pair.Value)
    Next
    If ListID.Count = ClRowCount Then
        btnAttend.Enabled = True
    Else
        btnAttend.Enabled = False
    End If
End Sub