我在这个主题中发现了很多问题,但不是这个问题:我正在使用c#应用程序设置,但是将每个设置保存到新行中变得非常难看。我尝试使用以下代码保存:
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?)
我可以动态保存设置,还是必须逐个保存每个设置?
答案 0 :(得分:4)
SelectedTab.Controls
返回ControlCollection
,因此索引器返回Control
。你需要施展它:
((CheckBox)settingsTabControl.SelectedTab.Controls[j]).Checked ...
或者如果它是RadioButton
你在追求:
((RadioButton)settingsTabControl.SelectedTab.Controls[j]).Checked ...