在组框

时间:2017-04-13 13:31:16

标签: .net vb.net

我们正在学校做一个项目,在我的程序中,我们有两个单选按钮(是/否),这些按钮位于一个面板中,而这些面板位于一个组框内。如何从不同的单选按钮获取值呢?我只是想看一个真/假值。它就像一个简单的列表,列出了用户应该能够说出的几个健康问题,如果他们有或没有,这将被发送到数据库。

我试图循环遍历每个复选框并将每个结果附加到String然后返回该String,但它绝对没有返回任何内容。甚至没有错误信息!我怀疑它可能与我在表格中循环通过两个单选按钮并检查两者上的if语句这一事实有关,但我不知道如何做到这一点。每个面板将包含两个单选按钮,仅此而已,是否可以通过索引找到它们?

布局有点像这样

+----------------+
|   Y N          |
|+-----+         |
||  o o|         |
||Panel|         |
|+-----+         |
|Groupbox        |  
+----------------+

两个o代表复选框(Y / N代表是/否)。

这是我用来遍历所有内容的code

Imports MySql.Data.MySqlClient

Public Class Egenerklaring
    ' The list of booleans that is supposed to be formatted correctly and sent to the database 
    Private verdier As List(Of Boolean) = New List(Of Boolean)
    ' A counter of how many checkboxes which is not checked at all 
    Private ikkeUtfylt As Integer

    Private Function skrivUtListe()
        Dim temp As List(Of String) = New List(Of String)
        For Each a As Boolean In verdier
            temp.Add(a.ToString)
        Next
        Return String.Join(",", temp)
    End Function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim i As Integer = 0
        For Each a As Control In Me.Controls
            If TypeOf a Is GroupBox Then
                For Each b As Control In a.Controls
                    If TypeOf b Is Panel Then
                        For Each c As Control In b.Controls
                            If TypeOf c Is CheckBox Then
                                ' If checkbox name contains ja / yes 
                                If c.Name.Contains("ja") AndAlso DirectCast(c, CheckBox).Checked Then
                                    verdier(i) = True
                                    ' If checkbox name contains nei / no 
                                ElseIf c.Name.Contains("nei") AndAlso DirectCast(c, CheckBox).Checked Then
                                    verdier(i) = False
                                Else
                                    ikkeUtfylt += 1
                                End If
                            End If
                            i += 1
                        Next
                    End If
                Next
            End If
        Next

        If ikkeUtfylt > 0 Then
            MsgBox("Fyll ut alle boksene!")
        Else
            MsgBox(Me.skrivUtListe())
        End If

    End Sub

End Class   

我真的被困住了,并且会感激任何帮助。

3 个答案:

答案 0 :(得分:0)

RadioButton2.Checked 如果您知道其名称,将在表单中的任何位置返回 True 。如果您不应该知道它的名字,那么只需:

Dim RadioChecked as boolean
For Each gb As Control In Me.Controls
    If TypeOf gb Is GroupBox Then
        For Each tb As Control In gb.Controls
            If TypeOf tb Is Panel Then
                For Each ctr As Control In tb.Controls
                    If TypeOf ctr Is RadioButton Then
                        RadioChecked = ctr.Checked
                    EndIF
                Next
            End If
        Next
    End If
Next

答案 1 :(得分:0)

The simplest way to get the values of your RadioButtons is to use a recursive method:

Private verdier As List(Of Boolean) = New List(Of Boolean)

Private Sub GetRadionButtonCheckStatus(ByVal parent As Control)

    For Each c As Control In parent.Controls
        If TypeOf (c) Is Panel Then
            For Each rb As RadioButton In c.Controls.OfType(Of RadioButton)()
                verdier.Add(rb.Checked)
            Next
        Else
            If c.HasChildren Then
                GetRadionButtonCheckStatus(c)
            End If
        End If
    Next

End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

    verdier.Clear()

    GetRadionButtonCheckStatus(Me)

    'Used for debugging
    For Each value In verdier
        Debug.WriteLine(String.Format("Value: {0}", value.ToString()))
    Next

End Sub

The output would look something similar to this:

Value: True
Value: False

You may need to extend on this and use a Dictionary(Of String, Boolean) and collect the RadioButton.Name property so you can correctly identify the status of each RadionButton:

Private verdier As Dictionary(Of String, Boolean) = New Dictionary(Of String, Boolean)

Private Sub GetRadionButtonCheckStatus(ByVal parent As Control)

    For Each c As Control In parent.Controls
        If TypeOf (c) Is Panel Then
            For Each rb As RadioButton In c.Controls.OfType(Of RadioButton)()
                verdier.Add(rb.Name, rb.Checked)
            Next
        Else
            If c.HasChildren Then
                GetRadionButtonCheckStatus(c)
            End If
        End If
    Next

End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

    verdier.Clear()

    GetRadionButtonCheckStatus(Me)

    'Used for debugging
    For Each kvp In verdier
        Debug.WriteLine(String.Format("Key: {0} Value: {1}", kvp.Key, kvp.Value))
    Next

End Sub

Using this code your output would look similar to this:

Key: RadioButton1 Value: True
Key: RadioButton2 Value: False

答案 2 :(得分:0)

使用扩展方法获取Form上的所有控件,无论它们是如何嵌套在容器中。然后使用LINQ

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim rbs = Me.ChildControls(Of RadioButton)()
        Dim groups = rbs.GroupBy(Of String)(Function(rb) rb.Name.Replace("ja", "").Replace("nei", ""))
        If groups.Select(Function(g) g.Sum(Function(i) If(i.Checked, 1, 0))).Sum() <> groups.Count Then
            MessageBox.Show("Fyll ut alle boksene!")
        Else
            Dim result =
                groups.
                Select(
                    Function(g)
                    Return g.First().Name.Replace("ja", "").Replace("nei", "") &
                        "=" & g.Where(Function(i) i.Name.Contains("ja")).First().Checked
                End Function)
            MessageBox.Show(String.Join(", ", result))
        End If

    End Sub

End Class

Module code

    <System.Runtime.CompilerServices.Extension()>
    Public Function ChildControls(Of T As Control)(ByVal parent As Control) As List(Of T)
        Dim result As New List(Of T)
        For Each ctrl As Control In parent.Controls
            If TypeOf ctrl Is T Then result.Add(CType(ctrl, T))
            result.AddRange(ctrl.ChildControls(Of T)())
        Next
        Return result
    End Function

End Module

enter image description here

请注意,结果中包含没有&#34; ja&#34;的RadioButtons的名称。 /&#34; nei&#34;。您可以在RadioButton的Tag属性中保留参数名称(数据库列?)并返回它,甚至将RadioButton名称设置为&#34; jaParameter1&#34;,&#34; neiParameter1&#34;等。 - 那是我怎么做的。