110,529
社区成员
发帖
与我相关
我的任务
分享
//下面代码写在FormA里面
public event EventHandler<GotFileEventArgs> GotFile;
public class GotFileEventArgs : EventArgs {
public string FilePath
{
get;
set;
}
}
private void OnGotFile(GotFileEventArgs e) {
if (null != GotFile) {
GotFile(this, e);
}
}
// 这段代码放在FormB的初始化里面(或者放在能访问到FormA的地方)...
// 这段代码执行完FormA里面的GotFile就不是空了。
Form1 form = new Form1();
form.GotFile += form_GotFile;
// 这里放在FormB里面,从参数e里面就能得到文件路径了
private void form_GotFile(object sender, Form1.GotFileEventArgs e)
{
MessageBox.Show(e.FilePath);
}
//下面代码写在FormA里面
public event EventHandler<GotFileEventArgs> GotFile;
public class GotFileEventArgs : EventArgs {
public string FilePath
{
get;
set;
}
}
private void OnGotFile(GotFileEventArgs e) {
if (null != GotFile) {
GotFile(this, e);
}
}
// 这段代码放在FormB的初始化里面(或者放在能访问到FormA的地方)...
// 这段代码执行完FormA里面的GotFile就不是空了。
Form1 form = new Form1();
form.GotFile += form_GotFile;
// 这里放在FormB里面,从参数e里面就能得到文件路径了
private void form_GotFile(object sender, Form1.GotFileEventArgs e)
{
MessageBox.Show(e.FilePath);
}
[/quote]
我的form1其实是别的地方new的,这两个form不是从属关系,是并列关系,他们两个被放在一个tabcontrol里面
private void OnGotFile(GotFileEventArgs e) {
if (null != GotFile) {
GotFile(this, e);
}
}
//这个是事件
public event EventHandler<GotFileEventArgs> GotFile;
public class GotFileEventArgs : EventArgs {
public string FilePath
{
get;
set;
}
}
// 像FormA注册事件
Form1 form = new Form1();
form.GotFile += form_GotFile;
// 这是事件里面
private void form_GotFile(object sender, Form1.GotFileEventArgs e)
{
MessageBox.Show(e.FilePath);
}