如何获取WinForm控件的IsChecked属性?
找不到一个看似简单的问题的答案。我需要遍历表单上的控件,如果控件是复选框并且被选中,则应该执行某些操作。像这样的东西
foreach (Control c in this.Controls)
{
if (c is CheckBox)
{
if (c.IsChecked == true)
// do something
}
}
但我无法到达 IsChecked 属性。
错误是“System.Windows.Forms.Control”不包含“IsChecked”的定义,并且找不到接受“System.Windows.Forms.Control”类型的第一个参数的扩展方法“IsChecked”(您是否缺少using 指令或程序集引用?)
我如何才能访问此属性?预先非常感谢!
编辑
好的,回答所有问题 - 我尝试过强制转换,但不起作用。
Can't find the answer to a seemingly easy question. I need to iterate through the controls on a form, and if a control is a CheckBox, and is checked, certain things should be done. Something like this
foreach (Control c in this.Controls)
{
if (c is CheckBox)
{
if (c.IsChecked == true)
// do something
}
}
But I can't reach the IsChecked property.
The error is 'System.Windows.Forms.Control' does not contain a definition for 'IsChecked' and no extension method 'IsChecked' accepting a first argument of type 'System.Windows.Forms.Control' could be found (are you missing a using directive or an assembly reference?)
How can I reach this property? Thanks a lot in advance!
EDIT
Okay, to answer all - I tried casting, it doesn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你很接近了。您要查找的属性已已选中
You're close. The property you're looking for is Checked
您需要将其投射到复选框。
You need to cast it to checkbox.
您必须添加从 Control 到 CheckBox 的强制转换:
You have to add a cast from Control to CheckBox:
您需要投射控件:
You need to cast the control:
Control 类没有定义
IsChecked
属性,因此您需要首先将其转换为适当的类型:The Control class does not define an
IsChecked
property, so you will need to cast it to the appropriate type first: