-
手搓组件
-
创建控件分为三步:
-
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; // 其中的一个控件 } }
-