.net 获取组框中面板中单选按钮的选中值
我们正在学校做一个项目,在我的程序中,我们有两个单选按钮(是/否),在一个面板中,这些面板在一个分组框中。那么,如何从不同的单选按钮中获取值?我只是想得到一个真/假值。这就像一个简单的列表,列出了用户应该能够说出的几个健康问题,这些问题将被发送到数据库 我试图循环检查每个复选框,并将每个结果附加到一个字符串,然后返回该字符串,但它完全不返回任何结果。甚至没有错误消息!我怀疑这可能与我在表单中循环两个单选按钮并检查两个单选按钮上的if语句有关,但我不知道如何否则做。每个面板将包含两个单选按钮,仅此而已,是否可以通过索引找到它们 布局有点像这样.net 获取组框中面板中单选按钮的选中值,.net,vb.net,.net,Vb.net,我们正在学校做一个项目,在我的程序中,我们有两个单选按钮(是/否),在一个面板中,这些面板在一个分组框中。那么,如何从不同的单选按钮中获取值?我只是想得到一个真/假值。这就像一个简单的列表,列出了用户应该能够说出的几个健康问题,这些问题将被发送到数据库 我试图循环检查每个复选框,并将每个结果附加到一个字符串,然后返回该字符串,但它完全不返回任何结果。甚至没有错误消息!我怀疑这可能与我在表单中循环两个单选按钮并检查两个单选按钮上的if语句有关,但我不知道如何否则做。每个面板将包含两个单选按钮,仅此
+----------------+
| Y N |
|+-----+ |
|| o o| |
||Panel| |
|+-----+ |
|Groupbox |
+----------------+
其中两个o代表复选框(Y/N代表是/否)
这是我用来循环所有内容的方法:
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
我真的被卡住了,非常感谢您的帮助。RadioButton2.选中的将返回真的,只要您知道它的名称。如果你不应该知道它的名字,那么简单地说:
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
RadioButton2.选中的将返回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
获取
单选按钮值的最简单方法是使用递归方法:
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
输出将类似于以下内容:
Value: True
Value: False
Key: RadioButton1 Value: True
Key: RadioButton2 Value: False
您可能需要在此基础上进行扩展,并使用字典(字符串、布尔值)
收集RadioButton.Name
属性,以便正确识别每个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
使用此代码,您的输出将如下所示:
Value: True
Value: False
Key: RadioButton1 Value: True
Key: RadioButton2 Value: False
获取单选按钮值的最简单方法是使用递归方法:
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
输出将类似于以下内容:
Value: True
Value: False
Key: RadioButton1 Value: True
Key: RadioButton2 Value: False
您可能需要在此基础上进行扩展,并使用字典(字符串、布尔值)
收集RadioButton.Name
属性,以便正确识别每个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
使用此代码,您的输出将如下所示:
Value: True
Value: False
Key: RadioButton1 Value: True
Key: RadioButton2 Value: False
使用扩展方法获取窗体上的所有控件,无论它们嵌套在容器中的程度如何。然后使用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
公共类表单1
私有子按钮1\u单击(发送者作为对象,e作为事件参数)处理按钮1。单击
Dim rbs=Me.ChildControls(单选按钮)()
Dim groups=rbs.GroupBy(字符串的)(函数(rb)rb.Name.Replace(“ja”和“”).Replace(“nei”和“”)
If groups.Select(函数(g)g.Sum(函数(i)If(i.Checked,1,0))).Sum()groups.Count然后
Show(“Fyll ut alle boksene!”)
其他的
模糊结果=
组。
挑选(
职能(g)
返回g.First().Name.Replace(“ja”,“”)。Replace(“nei”,“”)&
“=”&g.Where(函数(i)i.Name.Contains(“ja”)).First()已选中
终端功能)
MessageBox.Show(String.Join(“,”,result))
如果结束
端接头
末级
模块代码
公共函数ChildControls(属于T作为控件)(ByVal parent作为控件)作为列表(属于T)
Dim结果作为新列表(共T个)
对于父控件中的每个ctrl As控件
如果TypeOf ctrl是T,那么result.Add(CType(ctrl,T))
result.AddRange(ctrl.ChildControls(Of T)()
下一个
返回结果
端函数
端模块
请注意,结果包含不带“ja”/“nei”的单选按钮的名称。您可以交替地在RadioButton的Tag属性中保留参数名(数据库列?)并返回该名称,甚至可以将RadioButton命名为“jaParameter1”、“neiParameter1”等。我就是这样做的。使用扩展方法获取表单上的所有控件,无论它们在容器中有多嵌套。然后使用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
公共类表单1
私有子按钮1\u单击(发送者作为对象,e作为事件参数)处理按钮1。单击
Dim rbs=Me.ChildControls(单选按钮)()
Dim groups=rbs.GroupBy(字符串的)(函数(rb)rb.Name.Replace(“ja”和“”).Replace(“nei”和“”)
If groups.Select(函数(g)g.Sum(函数(i)If(i.Checked,1,0))).Sum()groups.Count然后
Show(“Fyll ut alle boksene!”)
其他的
模糊结果=
组。
挑选(
职能(g)
返回g.First().Name.Replace(“ja”,“”)。Replace(“nei”,“”)&
“=”&g.Where(函数(i)i.Name.Contains(“ja”)).First()已选中
终端功能)
MessageBox.Show(String.Join(“,”,result))
如果结束
端接头
末级
模块代码
公共函数ChildControls(属于T作为控件)(ByVal parent作为控件)作为列表(属于T)
Dim结果作为新列表(共T个)
对于父控件中的每个ctrl As控件
如果TypeOf ctrl是T,那么result.Add(CType(ctrl,T))
result.AddRange(ctrl.ChildControls(Of T)()
下一个
返回结果
端函数
端模块
请注意,结果包含不带“ja”/“nei”的单选按钮的名称。您可以交替地在RadioButton的Tag属性中保留参数名(数据库列?)并返回该参数,甚至可以将RadioButton命名为“jaParameter1”、“neiParameter1”等。我就是这样做的。不清楚您到底想做什么。您首先说您正在使用radio按钮
控件,然后继续使用复选框
控件。从你的代码中我可以看到