Winform—手搓控件和如何获取窗体上的控件

  • 手搓组件

    • 创建控件分为三步:

      • 1. 创建与组件相对应的对象 Button b1 = new Button();

      • 2. 添加组件属性和绑定事件 b1.BackColor = Color.Pink; b1.Text = "迪迦"; b1.Location = new Point(10, 10); //Location属性 设置按钮距离屏幕的坐标,参数是一个point点 。 // 代码添加绑定事件 b1.Click += new System.EventHandler(btn_click);

      • 3. 使用Controls.Add 方法进行添加 this.Controls.Add(b1)

      添加到离开窗体上或者其他容器控件内 this.Controls当窗体控件

  • 获取窗体上的控件

    • 方法1
      • 方法1 可以直接使用组件名称打点使用该组件或设置属性

      • Button1.Color = Color.Blue;

    • 方法2
      • 使用 Controls方法遍历寻找出自己需要的组件 this.Controls (窗体汇总所有的控件 集合)

    • for (int i = 0; i < this.Controls.Count; i++)
                  {
                      // 判断出Button组件 将它设为蓝色
                      if (Controls[i] is Button)
                      {
                          Controls[i].BackColor = Color.Blue;
                      }
                      // 其余组件设为红色
                      else
                      {
                          this.Controls[i].BackColor = Color.Red; // 其中的一个控件
                      }
                      
                  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值