2014-04-22 39 views
0

我需要諮詢...循環通過動態單選按鈕,如果它檢查狀態Y/N

我有形式,包含生成面板與用戶控件號(用戶控件包含在運行時產生的單選按鈕)。 http://tinypic.com/r/vq4kds/8(WinForm的結構)

我看到這個線程:loop through a dynamic form and panel and check if radio buttons check or not

值從MySQL數據庫分配的,所以我需要檢查這個單選按鈕與數據庫。還無線電的名稱是由循環分配: 來自:

"radioButton1_"+[i].ToString(); 

到:

"radioButton4_"+[i].ToString(); 

例如: 我將有10組4個單選按鈕。 (40個無線電)我應該將他們的狀態保存到數組中嗎? 像這樣[0,0,0,1,1,0,0,0,...]。我需要機會根據數據庫進行檢查。

我欣賞一些簡單的解決方案。

回答

1

我的建議是在winform中循環顯示面板。對於每個面板,獲取usercontrol。然後,遍歷usercontrol中的每個控件。如果該控件是一個RadioButton,則根據數據庫進行檢查。我不知道你是如何將單選按鈕與數據庫相匹配的,但也許RadioButton的Text屬性是匹配的。

僞代碼:

foreach Control panel in winform.Controls: 
    // Type check 
    if panel is Panel: 
     // Get the user control from the panel 
     UserControl usrCtrl = panel.usercontrol as UserControl 
     foreach Control control in usrCtrl.Controls: 
      // Type check 
      if control is RadioButton: 
       // do database stuff 

編輯: 我建議從數據庫中賦值時設置你的單選按鈕的標籤屬性。標籤屬性可以容納任何你想要的東西。例如,您可以將標籤設置爲YourTableName.YourColumnName。這樣你可以跟蹤RadioButton對應的字段。

+0

對不起,我的錯誤。我說過,值是從數據庫中分配的,但那不完全正確。由於多線收音機的問題,我使用經典標籤作爲單選按鈕的描述。所以我只能檢查狀態(1/0)。 – Crouxe

+0

這個'UserControl usrCtrl = panel.usercontrol作爲UserControl'有正確的語法嗎? – Crouxe

+0

這是僞代碼。語法可能不正確,但意圖應該清楚。既然你沒有發佈太多的代碼,我不能比這更具體。 – ErikusMaximus