如何将选中的复选框项放入String数组中

时间:2017-05-05 12:26:51

标签: vb.net

这是我想要做的事情,我想把所有项目都选中或选中复选框并将其放入 然后,字符串数组通过按钮的触发器在动态标签数组上显示它。 这是我到目前为止尝试过的代码......

Dim arraySize As Integer
Dim lbl() As Label
Dim str() As String

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Label1.Text = arraySize
End Sub
Private Sub chkOption1_CheckedChanged(sender As Object, e As EventArgs) Handles chkOption1.CheckedChanged
    If chkOption1.Checked = True Then
        arraySize = arraySize + 1
    Else
        arraySize = arraySize - 1
    End If
    Label1.Text = arraySize
End Sub
Private Sub chkOption2_CheckedChanged(sender As Object, e As EventArgs) Handles chkOption2.CheckedChanged
    If chkOption2.Checked = True Then
        arraySize = arraySize + 1
    Else
        arraySize = arraySize - 1
    End If
    Label1.Text = arraySize
End Sub
Private Sub chkOption3_CheckedChanged(sender As Object, e As EventArgs) Handles chkOption3.CheckedChanged
    If chkOption3.Checked = True Then
        arraySize = arraySize + 1
    Else
        arraySize = arraySize - 1
    End If
    Label1.Text = arraySize
End Sub
Private Sub chkOption4_CheckedChanged(sender As Object, e As EventArgs) Handles chkOption4.CheckedChanged
    If chkOption4.Checked = True Then
        arraySize = arraySize + 1
    Else
        arraySize = arraySize - 1
    End If
    Label1.Text = arraySize
End Sub
Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click
    ReDim lbl2(arraySize)
    ReDim str(arraySize)
    For ctr = 1 To lbl2.Length - 1
        lbl(ctr) = New Label
        lbl(ctr).Width = 60
        lbl(ctr).Height = 40
        If ctr = 1 Then
            lbl(ctr).Left = 20
        Else
            lbl(ctr).Left = ((lbl2(ctr).Width + 20) * ctr) - 60
        End If
        lbl(ctr).Top = Me.Height - lbl(ctr).Height * (5 / 2)
        Me.Controls.Add(lbl(ctr))
    Next
    For ctrs = 1 To gn.Length - 1
        If chkAction.Checked = True Then
            str(ctrs) = "Action"
        End If
        If chkFantasy.Checked = True Then
            str(ctrs) = "Fantasy"
        End If
        If chkMystery.Checked = True Then
            str(ctrs) = "Mystery"
        End If
        If chkWar.Checked = True Then
            str(ctrs) = "War"
        End If
        lbl(ctrs).Text = str(ctrs)
    Next
End Sub

问题是,当我选择或选中一个或多个复选框时,就是那个 标签中显示的始终是根据其顺序的最后一个复选框。例如,我检查了chkOption1,chkOption3, chkOption2,标签上的输出都是" chkOption3"。如果我检查了一个项目并按下按钮,它将会一次 在标签中显示我想要的正确值。但我想要做的是如果我选择一个或多个 复选框只需在按钮中单击即可显示标签中的所有值,例如我选择option1,option4,option3,option2,然后它将 在标签中显示选项1到4的所有值...那我怎么能这样做?请帮忙。提前谢谢......

2 个答案:

答案 0 :(得分:0)

我会选择List(Of T)For Each

此处的原则是检查类型CheckBox的每个控件并检索其值并将其添加到列表中。

  

如果复选框位于GroupBox内,请注意这不起作用   或其他容器。如果是这种情况,那么你需要循环   容器的所有控件。

Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click
    Label1.Text = ""
    Dim SelectedGenres As List(Of String)
    SelectedGenres = GetGenres()
    For Each Genre As String In SelectedGenres
        Label1.Text = Label1.Text & vbNewLine & Genre
    Next
End Sub

然后会使用这样的函数:

Public Function GetGenres() As List(Of String)
    Dim Genres As New List(Of String)
    Genres.Clear()
    For Each ctrl As Control In Me.Controls
        'If the control is of type Checkbox
        Dim CurrentCheckbox As CheckBox
        If TypeOf ctrl Is CheckBox Then
            CurrentCheckbox = ctrl
            If CurrentCheckbox.Checked = True Then
                Genres.Add(CurrentCheckbox.Text)
            End If
        End If
    Next
    Return Genres
End Function

答案 1 :(得分:0)

我强烈建议您使用列表而不是数组,特别是因为大小不同。您不需要保持数组的大小。只需使用List(Of String)

Dim selectedActions As New List(Of String)

If chkAction.Checked Then selectedActions.Add("Action")
If chkFantasy.Checked Then selectedActions.Add("Fantasy")
If chkMystery.Checked Then selectedActions.Add("Mystery")
If chkWar.Checked Then selectedActions.Add("War")

然后你得到了你的arraySize

selectedAction.Count

只需循环该列表即可获取信息

For ctr = 0 To selectedActions.Count-1
    Dim lbl As New Label
    lbl.Width = 60
    lbl.Height = 40
    If ctr = 1 Then
        lbl.Left = 20
    Else
        lbl.Left = ((lbl2(ctr).Width + 20) * ctr) - 60
    End If
    lbl.Top = Me.Height - lbl.Height * (5 / 2)
    Me.Controls.Add(lbl)

    lbl.Text = selectedActions(ctrs)
Next

如果最大值的数量是固定的。如果没有值,您可以更容易地添加空标签并隐藏/显示或将.Text设置为“”。在您的代码中,您每次都会创建新标签。