欢迎光临散文网 会员登陆 & 注册

[C#学习笔记17]控件集合、事件统一关联、Tag数据存、对象与集合综合

2020-06-18 21:05 作者:技术龙的传人  | 我要投稿

事件的集中响应

    原理:就是相同的控件,可以关联同一个事件响应方法。

    好处:我们可以集中处理数据

    核心内容:按钮的集中添加和Tag数据的封装、窗体Controls集合的用途、通过遍历Controls集合优化事件关联。

事件通用处理中数据的获取

    核心内容:在按钮事件中获取Tag数据的方法、对象的封装、泛型集合List运用

按钮属性:边框FlatStyle选择Flat扁平化,背景色蓝色,字体白色微软雅黑加粗小四

Tag填入数据,比如1111,11

用之前写过的类Course

 /// <summary>

    /// 课程类

    /// </summary>

    public class Course 

    {

        public Course() { }

        public Course(int courseId, string courseName, int classHour, string teacher)

        {

            this.CourseId = courseId;

            this.CourseName = courseName;

            this.ClassHour = classHour;

            this.Teacher = teacher;

        }

        public int CourseId { get; set; }//课程编号

        public string CourseName { get; set; }//课程名称

        public int ClassHour { get; set; }//课时

        public string Teacher { get; set; }//主讲老师

}

    public partial class FrmEventApp : Form

    {

        //用来封装课程对象的容器

        private List<Course> courseList = new List<Course>();

        public FrmEventApp()

        {

            InitializeComponent();

            //多个按钮响应同一个事件,在此做事件关联

            //this.btn01.Click += new System.EventHandler(this.btn_Click);

            //this.btn02.Click += new System.EventHandler(this.btn_Click);

            //this.btn03.Click += new System.EventHandler(this.btn_Click);

            //this.btn04.Click += new System.EventHandler(this.btn_Click);

            //this.btn05.Click += new System.EventHandler(this.btn_Click);

            //this.btn06.Click += new System.EventHandler(this.btn_Click);

            //this.btn07.Click += new System.EventHandler(this.btn_Click);

            //this.btn08.Click += new System.EventHandler(this.btn_Click);

            //this.btn09.Click += new System.EventHandler(this.btn_Click);

            //this.btn10.Click += new System.EventHandler(this.btn_Click);

            //this.btn11.Click += new System.EventHandler(this.btn_Click);

            //this.btn12.Click += new System.EventHandler(this.btn_Click);

            //以上方法,如果你这么写程序,会被别人认为你什么都不懂!

            foreach (Control item in this.Controls)

            {

                //if (item is Button)//通过控件类型过滤不需要的控件

                //{

                //    Button btn = item as Button;//转换成Button

                //    if (btn.Tag.ToString() != "Save")//过滤不需要的按钮,特别注意Tag的使用

                //    {

                //        btn.Click += new System.EventHandler(this.btn_Click);

                //    }

                //}

                if (item is Button && item.Tag.ToString() != "Save")

                {

                    item.Click += new System.EventHandler(this.btn_Click);

                }

            }

        }

        //事件集中处理方法

        private void btn_Click(object sender, EventArgs e)

        {

            Button btn = sender as Button;

            //将当前按钮Tag属性中封装的课程信息,通过字符串分割得到

            string[] info = btn.Tag.ToString().Split(',');

            //将当前课程信息封装到课程对象,并将课程对象封装到集合中

            this.courseList.Add(new Course

            {

                CourseName = btn.Text,

                CourseId = Convert.ToInt32(info[0]),

                ClassHour = Convert.ToInt32(info[1])

            });

            //改变当前按钮的背景色

            btn.BackColor = Color.Green;

            //请大家思考:如果避免用户多次添加同一个课程按钮,而导致多次添加的问题...

       }

        //保存所选课

        private void btnSave_Click(object sender, EventArgs e)

        {

            //实际开发中,保存可以到数据库、文件...

            //测试看看所选择的课程

            foreach (var item in this.courseList)

            {

                Console.WriteLine(item.CourseId+"\t"+item.ClassHour+"\t"+item.CourseName);

            }

        }

    }


[C#学习笔记17]控件集合、事件统一关联、Tag数据存、对象与集合综合的评论 (共 条)

分享到微博请遵守国家法律