遍历并更改所有表单对象

时间:2019-03-29 15:10:40

标签: c# winforms

我创建了一个带有6张“卡片”的Windows窗体应用程序。 “卡”是我从System.Windows.Forms.PictureBox派生的类。 我添加了一个名为“ id”的成员。

有没有办法遍历所有类型为Card的对象并更改id的值?

Screenshot

类定义在下面。

    public class Card : System.Windows.Forms.PictureBox
    {
        System.Drawing.Bitmap Back = MemoryGame.Properties.Resources.MG_back;

        byte id = 0;

        public Card()
        {

            this.Image = Back;

            this.SizeMode = PictureBoxSizeMode.Zoom;

         }

        public void Flip(System.Drawing.Bitmap bitmap)
        {

            if (this.Image == Back)
            {
                this.Image = bitmap;
            }
            else
            {
                this.Image = Back;
            }
        }

        public int GetId()
        {
            return this.id;
        }

    }

2 个答案:

答案 0 :(得分:1)

您可以遍历recurrent_layer_normalization集合,例如:

Controls

答案 1 :(得分:1)

环回parentcontrols的“ Controls”属性。然后,您可以检查当前控件是否为Card类型。

let input=[{Stats:"Number of virus","Dec-2018":"1"},{Stats:"Number of alerts","Dec-2018":"2"},{Stats:"Number of incidents","Dec-2018":"1"},{Stats:"Vulnerabilities","Dec-2018":"6"},{Stats:"Number of virus","Nov-2018":"4"},{Stats:"Number of alerts","Nov-2018":"6"},{Stats:"Number of incidents","Nov-2018":"2"},{Stats:"Vulnerabilities","Nov-2018":"8"},{Stats:"Number of virus","Oct-2018":"2"},{Stats:"Number of alerts","Oct-2018":"2"},{Stats:"Number of incidents","Oct-2018":"5"},{Stats:"Vulnerabilities","Oct-2018":"2"}];

let merged = input.reduce((acc, o) => {
  acc[o.Stats] = { ...acc[o.Stats], ...o }
  return acc;
}, {})

console.log(Object.values(merged))

编辑:本示例中使用的类型模式从C#7.0开始可用