2

我在这个主题中发现了很多问题,但不是这个:我正在使用 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?)

我可以动态保存设置,还是必须逐个保存每个设置?

4

1 回答 1

4

SelectedTab.Controls返回 a ControlCollection,因此索引器返回 a Control。你需要投射它:

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

或者如果它是RadioButton你所追求的:

((RadioButton)settingsTabControl.SelectedTab.Controls[j]).Checked ...
于 2013-10-23T17:24:20.790 回答