form1:
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:
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 和
public Form2()
{
InitializeComponent();
}
这样Form2中的代码就可以像Form1中的代码一样使用了!
运行结果: