2017-04-13 55 views
0

我們在學校和項目中正在進行一個項目,我們在面板中有兩個單選按鈕(是/否),這些面板位於組框中。我如何從不同的單選按鈕獲取值?我只是想看到一個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 

我真的很堅持,並會在所有得到任何幫助。

+0

目前尚不清楚你正在嘗試做什麼。你首先說你正在使用'RadionButton'控件,但是接着使用'CheckBox'控件。從你的代碼中我可以看到'CheckBox',但是我不確定問題是什麼。嘗試'verdier.Add(True)'和'verdier.False(False)'。也可能值得考慮像我的[回覆](http://stackoverflow.com/a/43319283/6375113),你將不得不適應的遞歸方法。這節省了不得不放下每個容器控制器。 – Bugs

+0

哦,對不起。那麼它是有道理的,它不會返回任何東西,哈哈。我繼續混合CheckBox和RadioButton。 – gloriousCatnip

+0

以下是一些更新的代碼:https://gist.github.com/wOstensen/552c42f8ed18194387cf1b6599d3c6dd – gloriousCatnip

回答

0

RadioButton2.Checked將返回正確無論它在表單中的任何位置,如果您知道它的名稱。如果傻冒不應該知道它的名字那麼簡單:

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 
0

最簡單的辦法讓你的RadioButtons是使用遞歸方法的值:

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 

您可能需要延長這一點,並使用Dictionary(Of String, Boolean)和收集RadioButton.Name屬性,以便您可以正確標識entify每個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 

使用此代碼的輸出將類似於此:

Key: RadioButton1 Value: True 
Key: RadioButton2 Value: False 
+0

對不起,我一段時間沒有迴應,一直在與該項目合作等。我找到了一個我認爲可行的解決方案。 我認爲,因爲每個面板都有兩個單選按鈕,所以我應該可以爲Ja-radio執行 'Panel.Controls(0)',對Nei執行1。 這是我現在的代碼:https://gist.github.com/wOstensen/4111ba0d7bf31d342d30b26329e1f421 它用於檢查是否有任何未選中,但它不會返回正確的結果。這是一張圖片,顯示我選擇了什麼結果:http://imgur.com/a/J4s8d 爲什麼幾乎所有的東西都是假的? – gloriousCatnip

0

使用擴展方法來獲得表單上的所有控件,無論怎樣嵌套在他們是容器。然後使用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

注意,結果中包含的單選按鈕的名稱,而不 「JA」/ 「巾幗不讓鬚眉」。您可以交替地在RadioButton的Tag屬性中保存參數名稱(數據庫列?)並返回該名稱,甚至可以製作RadioButton名稱,例如「jaParameter1」,「neiParameter1」等 - 這就是我的做法。

相關問題