如何获取WinForm控件的IsChecked属性?

发布于 2024-10-30 18:00:47 字数 548 浏览 5 评论 0原文

找不到一个看似简单的问题的答案。我需要遍历表单上的控件,如果控件是复选框并且被选中,则应该执行某些操作。像这样的东西

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

风追烟花雨 2024-11-06 18:00:47

你很接近了。您要查找的属性已已选中

foreach (Control c in this.Controls) {             
   if (c is CheckBox) {
      if (((CheckBox)c).Checked == true) 
         // do something             
      } 
} 

You're close. The property you're looking for is Checked

foreach (Control c in this.Controls) {             
   if (c is CheckBox) {
      if (((CheckBox)c).Checked == true) 
         // do something             
      } 
} 
梦境 2024-11-06 18:00:47

您需要将其投射到复选框。

foreach (Control c in this.Controls)
        {
            if (c is CheckBox)
            {
                if ((c as CheckBox).IsChecked == true)
                    // do something
            }
        }

You need to cast it to checkbox.

foreach (Control c in this.Controls)
        {
            if (c is CheckBox)
            {
                if ((c as CheckBox).IsChecked == true)
                    // do something
            }
        }
淡忘如思 2024-11-06 18:00:47

您必须添加从 Control 到 CheckBox 的强制转换:

foreach (Control c in this.Controls)
        {
            if (c is CheckBox)
            {
                if ((c as CheckBox).IsChecked == true)
                    // do something
            }
        }

You have to add a cast from Control to CheckBox:

foreach (Control c in this.Controls)
        {
            if (c is CheckBox)
            {
                if ((c as CheckBox).IsChecked == true)
                    // do something
            }
        }
归属感 2024-11-06 18:00:47

您需要投射控件:

    foreach (Control c in this.Controls)
    {
        if (c is CheckBox)
        {
            if (((CheckBox)c).IsChecked == true)
                // do something
        }
    }

You need to cast the control:

    foreach (Control c in this.Controls)
    {
        if (c is CheckBox)
        {
            if (((CheckBox)c).IsChecked == true)
                // do something
        }
    }
白衬杉格子梦 2024-11-06 18:00:47

Control 类没有定义 IsChecked 属性,因此您需要首先将其转换为适当的类型:

var checkbox = c as CheckBox;
if( checkbox != null )
{
    // 'c' is a CheckBox
    checkbox.IsChecked = ...;
}

The Control class does not define an IsChecked property, so you will need to cast it to the appropriate type first:

var checkbox = c as CheckBox;
if( checkbox != null )
{
    // 'c' is a CheckBox
    checkbox.IsChecked = ...;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文