C# Visual Studio 项目中,将一个Form1界面的一个cs源文件拆分成多个

关键字partial

  • 新建项目,添加两个button

这里写图片描述

  • 生成点击事件
        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("button1");
        }
        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("button2"); 
        }
  • 新建Main.cs文件,使用 partial
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("button2"); 
        }
    }
}
  • button1的点击事件留在Form1.cs中,button2的点击事件剪切到了Main.cs中
  • 完成,运行,收工
  • 大致的解释是,通过partial关键字,把Form1.cs和Main.cs两个文件拼接在了一起。

这里写图片描述

猜你喜欢

转载自blog.csdn.net/kangweijian/article/details/80333287