我們在學校和項目中正在進行一個項目,我們在面板中有兩個單選按鈕(是/否),這些面板位於組框中。我如何從不同的單選按鈕獲取值?我只是想看到一個True/False值。這就像一個用戶應該能夠說的幾個健康問題的簡單列表,如果他們有或沒有,這將被髮送到數據庫。獲取組框中單選按鈕的選中值
我試圖遍歷每個複選框,並將每個結果附加到一個字符串,然後返回該字符串,但它什麼都不返回。甚至沒有錯誤信息!我懷疑它可能與我在表單中循環兩個單選按鈕並檢查兩者上的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
我真的很堅持,並會在所有得到任何幫助。
目前尚不清楚你正在嘗試做什麼。你首先說你正在使用'RadionButton'控件,但是接着使用'CheckBox'控件。從你的代碼中我可以看到'CheckBox',但是我不確定問題是什麼。嘗試'verdier.Add(True)'和'verdier.False(False)'。也可能值得考慮像我的[回覆](http://stackoverflow.com/a/43319283/6375113),你將不得不適應的遞歸方法。這節省了不得不放下每個容器控制器。 – Bugs
哦,對不起。那麼它是有道理的,它不會返回任何東西,哈哈。我繼續混合CheckBox和RadioButton。 – gloriousCatnip
以下是一些更新的代碼:https://gist.github.com/wOstensen/552c42f8ed18194387cf1b6599d3c6dd – gloriousCatnip