gpt4 book ai didi

c# - 为什么我得到 "' System.Windows.Forms.Control' does not contain a definition for 'Checked' ..."here?

转载 作者:行者123 更新时间:2023-11-30 20:53:56 25 4
gpt4 key购买 nike

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

我可以动态保存设置,还是必须一项一项地保存设置?

最佳答案

SelectedTab.Controls 返回 ControlCollection因此索引器返回 Control .你需要转换它:

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

或者如果它是一个RadioButton,你会:

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

关于c# - 为什么我得到 "' System.Windows.Forms.Control' does not contain a definition for 'Checked' ..."here?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19548178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com