2013-10-23 85 views
2

我在這個主題中發現了很多問題,但不是這一個:我正在使用c#應用程序設置,但將每個設置保存在新行中變得非常難看。我試圖挽救這個用下面的代碼:爲什麼我在這裏得到''System.Windows.Forms.Control'沒有包含「Checked'...」的定義?

for (int j = 0; j < settingsTabControl.SelectedTab.Controls.Count; j++) 
{ 
    string currItemName = settingsTabControl.SelectedTab.Controls[j].Name; 
    if (currItemName.Substring(0, 7) == "savable" && currItemName == currOptionName) 
    { 
     if (savableRunAsAdmin.HasProperty("Text")) 
     { 
      settingsTabControl.SelectedTab.Controls[j].Text = currOptionValue; 
     } 
     else if (savableRunAsAdmin.HasProperty("Checked")) 
     { 
      settingsTabControl.SelectedTab.Controls[j].Checked = Convert.ToBoolean(currOptionValue); 
     } 
    } 
} 

public static bool HasProperty(this object objectToCheck, string methodName) 
{ 
    var type = objectToCheck.GetType(); 
    return type.GetProperty(methodName) != null; 
} 

但它說,這

'System.Windows.Forms.Control' does not contain a definition for 'Checked' and no extension method 'Checked' accepting a first argument of type 'System.Windows.Forms.Control' could be found (are you missing a using directive or an assembly reference?) 

我可以動態地保存設置,或者我必須保存每個設置一個接一個?

+1

演員到'CheckBox'或'RadioButton'。 –

回答

4

SelectedTab.Controls返回ControlCollection,索引器返回Control。你需要將它轉換:

((CheckBox)settingsTabControl.SelectedTab.Controls[j]).Checked ... 

,或者如果它是一個RadioButton你後:設置`Checked`前

((RadioButton)settingsTabControl.SelectedTab.Controls[j]).Checked ... 
相關問題