方式一、用委托作为形参,把结果传回实参
方式二、通过接口实现
方式三、通过事件关联,适用桌面应用程序
方式四、子窗体调用父窗体的函数(委托)


方式一、用委托作为形参,把结果传回实参

public partial class index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Child chld = new Child();
chld.GetURL((url) =>
{
Response.Write(url);
//取得结果:http://microsoft-zh.cn
});
}
}
public class Child
{
//创建委托
public delegate void GetURLDelegate(string url);
public void GetURL(GetURLDelegate func)
{
func("http://microsoft-zh.cn");
}
}

 方式二、通过接口实现

public partial class index : System.Web.UI.Page, ILoginCallback
{
protected void Page_Load(object sender, EventArgs e)
{
Child chld = new Child();
chld.GetURL(this);
}
public void OnGetURL(string url)
{
Response.Write(url);
//取得结果:http://microsoft-zh.cn
}
}
public class Child
{
public void GetURL(ILoginCallback callback)
{
callback.OnGetURL("http://microsoft-zh.cn");
}
} public interface ILoginCallback
{
/// <summary>
/// 接口函数
/// </summary>
void OnGetURL(string url);
}

方式三、通过事件关联,适用桌面应用程序

public partial class Form1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ } Form2 chld = new Form2();
protected void Button1_Click(object sender, EventArgs e)
{
chld.AuthReady += new AuthEventHandler(chld.OnAuthReady);
}
}
public partial class Form2 : System.Web.UI.Page
{
// 事件
public event AuthEventHandler AuthReady;
protected void Button2_Click(object sender, EventArgs e)
{
if (AuthReady != null)
{
AuthEventArgs args = new AuthEventArgs();
args.user_id = 1;
args.user_name = "micro";
AuthReady(this, args);
}
}
public void OnAuthReady(object sender, AuthEventArgs e)
{
Console.Write(e.user_name);
}
}
//委托事件
public delegate void AuthEventHandler(object sender, AuthEventArgs e);
public class AuthEventArgs : EventArgs
{
public int user_id { get; set; }
public string user_name { get; set; }
}

方式四、子窗体调用父窗体的函数(委托)
1、子窗体Form2

public partial class Form2 : Form
{
// 申明委托,与父窗体方法类型相同
public delegate string FunDelegate(int a);
// 用来接收父窗体方法的委托变量
public FunDelegate funDelegate;
private void button1_Click(object sender, EventArgs e)
{
if (funDelegate != null)
{
// 调用方法
funDelegate(2);
}
}
}

2、父窗体Form1

private void Form1_Load(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
//父窗体的方法传给子窗体
frm2.FunDelegate = Fun;
frm2.Show();
}
// 父窗体的方法
private string Fun(int a)
{
return "我是主窗体方法";
}

  

  

 

最新文章

  1. 十一个行为模式之解释器模式(Interpreter Pattern)
  2. No.018:4Sum
  3. 修改git commit默认触发的编辑器
  4. MyBatis3资料网址
  5. js的选择星级评分插件
  6. Codeforces Educational Codeforces Round 3 B. The Best Gift 水题
  7. Spring的事件处理
  8. linux下读取系统内存的demo
  9. JVM的GC(概念与深入)
  10. Ubuntu 18.04 下如何配置mysql 及 配置远程连接
  11. 【转】Git 代码行统计命令集
  12. JavaWeb学习 (四)————Http协议
  13. Ubuntu16.04下完美切换Python版本
  14. Entlib DAAB映射枚举类型
  15. offsetHeight、scrollHeight、clientHeight、height
  16. github添加ssh连接用户
  17. Socket和ServletSocket的交互
  18. Goroutines和Channels(五)
  19. 关联github, 添加gitignore 规则
  20. 如何用正确的姿势编写jQuery插件

热门文章

  1. SqlServer索引碎片
  2. 【C++】三大概念要分清--重载,隐藏(重定义,覆盖(重写)
  3. hdu 1757 和1005差不多 (矩阵快速幂)
  4. SpringMVC的初始
  5. BZOJ1042 [HAOI2008]硬币购物 完全背包 容斥原理
  6. Redis数据结构之list
  7. R从3.4升级到3.5
  8. Python String 方法详解
  9. pyqt5 &#39;QWidget&#39; object has no attribute &#39;setCentralWidget&#39;(转)
  10. Flutter开发环境(Window)配置及踩坑记录