partial

发布时间 2023-05-08 18:22:15作者: 阿霖找BUG

partial部分类,当两个类的类名一致,且都加了关键字partial,在编译时是当作一个类

partial class Form1//partial部分类
public partial class Form1 : Form

右键快捷菜单

把你当前想要的右键菜单选中,默认无

事件:

在.Net平台上,我们所用的控件都封装了很多的事件。

事件就是对用户操作的某一个行为进行封装,比如对按钮的点击事件进行封装,当用户点击按钮的时候就会触发事件,在事件里完成我们需要的任务。

事件:

双击对应的属性生成事件

 

        private void button1_Click(object sender, EventArgs e)//可以改变事件名字,其他参数之类不可更改
        {
            Button button=(Button)sender;
            //MessageBox.Show("text");//弹出弹窗
            MessageBox.Show(button.Tag.ToString());
        }

关联事件:

this.button1.Click += new System.EventHandler(this.button1_Click);//关联事件

取消关联

this.button1.Click -= new System.EventHandler(this.button1_Click);//取消关联

删除关联:右键选择重置(删除this.button1.Click += new System.EventHandler(this.button1_Click);)

初始化元素:

        //窗体所有控件和初始化完毕后执行的事件,通常不用
        private void FormMain_Load(object sender, EventArgs e)
        {

        }

在这里:

        public FormMain()
        {
            InitializeComponent();//调用Designer类中的方法,用于控件的初始化

            //初始化可以在这里(InitializeComponent();之后)
        }

关闭窗口事件:

        //窗台关闭前发生的
        private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            //Button button = (Button)sender;
            DialogResult result = MessageBox.Show("确定关闭吗", "关闭确认",
                MessageBoxButtons.OKCancel,
                MessageBoxIcon.Question);
            if (result == DialogResult.Cancel)
            {
                e.Cancel = true;
            }
        }


        //窗体关闭后发生的(可以用来触发关闭线程之类的)
        private void FormMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            DialogResult result = MessageBox.Show("已经关闭");

        }

事件的集中处理:

创建集合:

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

关联事件:

            //事件关联,一个个写出来,不推荐
            //this.button1.Click += new System.EventHandler(this.button1_Click);
            //this.button2.Click += new System.EventHandler(this.button1_Click);
            //this.button3.Click += new System.EventHandler(this.button1_Click);
            //this.button4.Click += new System.EventHandler(this.button1_Click);
            foreach (Control item in this.Controls)
            {
                if(item is Button && item.Tag.ToString() != "save")//筛除不是按钮和提交按钮
                //Tag事件在Control有,在object(var)中是没有的
                //假设要用object类型,则要转化为Button类型(Button button = (Button)sender;)
                {
                    item.Click += new System.EventHandler(this.button1_Click);//循环遍历事件关联
                }
            }

选项按钮事件:把tag数据储存到集合

        private void button1_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            //MessageBox.Show(button.Tag.ToString());
            string[] info=button.Tag.ToString().Split(',');

            this.courseList.Add(new Course//传递的是一个不确定有无参的量,则需要给父类一个无参构造方法
            {
                name = Convert.ToString(button.Text),
                id = Convert.ToInt32(info[0]),
                time = Convert.ToInt32(info[1])
            });
            button.BackColor = Color.White;//点击后按钮变色
        }

提交按钮事件:遍历集合

        private void button5_Click(object sender, EventArgs e)//提交按钮
        {
            foreach (Course item in this.courseList)
            {
                Console.WriteLine($"课程名称是{item.name},课程编号是{item.id},课程时长是{item.time}");
            }
        }