110,566
社区成员
发帖
与我相关
我的任务
分享
private void button1_Click(object sender, EventArgs e)
{
Form form2 = new Form2();
form2.OnFormClose+=(string strA){
this.textBox1.Text
}
form2.ShowDialog();
这么写会出错
}
在form2中
public delegate void CallFormClose(string strA);
public event CallFormClose OnFormClose;
private void button1_Click(object sender, EventArgs e)
{
if(OnFormClose==null);
{
OnFormClose(strA);
}
this.Close();
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 一个窗体对另一个窗体的数据进行修改
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Form2 f2 = new Form2();
private void button1_Click(object sender, EventArgs e)
{
f2.xiugai=new Form2.my(f2_Event);
f2.Show();
}
private void f2_Event(string str)
{
label1.Text = str;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 一个窗体对另一个窗体的数据进行修改
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public delegate void my(String str);//第二个窗体中设置委托
public my xiugai; //用委托定义一个 类似方法
//public event my xiugai ; //一样的效果
private void button1_Click(object sender, EventArgs e)
{
xiugai(textBox1.Text.ToString());
}
private void Form2_Load(object sender, EventArgs e)
{
}
}
}
这是常用的
另外一个方法 就是在类 之下 构造函数之前定义一个 internal的 静态字段。 当然这个类也需要改成internal的 然后在该类中把 你想传递的值赋给这个字段,
在另外一个类中,就可以 用类名.字段名的方法来获取这个值了 ,(string 也可以穿,连控件估计都能传,但是没有试)