C# 代码分两个窗体编写

2020-08-19 10:31:22 浏览数 (1)

form1:

代码语言:javascript复制
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
           int data= sumData(10, 25);
            MessageBox.Show(data.ToString());
        }
    }
}

form2:

代码语言:javascript复制
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp4
{
    public partial class Form1 : Form
    {
       public static int sumData(int a,int b)
        {
            return a   b;
        }
    }
}

此窗体的Form2:Form 改为Form1:Form

再删除Form2.Designer.sc 和

代码语言:javascript复制
public Form2()
        {
            InitializeComponent();
        }

这样Form2中的代码就可以像Form1中的代码一样使用了!

运行结果:

0 人点赞