问题描述:有2个UserControl:UserControl1 里有一个Button,UserControl2 里面有一个TextBox,这2个控件都加载到了主窗体Form1 上。要求的是,点击 UserControl1 的button 显示 UserControl2中TextBox输入的内容。

一般来讲有2种方式:

1. 公开属性

2. 声明事件

来看看这2种方式的操作代码:

1. 公开2个UserControl的属性。并在Form1中使用

public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent(); } public Button Btn // Define Btn as public
{
get
{
return this.button1;
}
}
}

UserControl1

 public partial class UserControl2 : UserControl
{
public UserControl2()
{
InitializeComponent();
} public TextBox textbox // define textbox as public
{
get
{ return this.textBox1; }
}
}

UserControl2

在From1中注册Button的事件

 public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.userControl11.Btn.Click += new EventHandler(Btn_Click);
} void Btn_Click(object sender, EventArgs e)
{
MessageBox.Show(this.userControl21.textbox.Text);
}
}

注册Button事件

2. 声明事件

public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent(); }
public event EventHandler BtnClick; //define one public Click event. private void button1_Click(object sender, EventArgs e)
{
BtnClick(sender, e); // just do it.
}
}

UserControl1

 public partial class UserControl2 : UserControl
{
public UserControl2()
{
InitializeComponent();
} public event Action<string> GetText; //define one action to get textbox's text value private void textBox1_TextChanged(object sender, EventArgs e)
{
GetText(textBox1.Text); // Get this text after input.
}
}

UserControl2

在Form1中调用

 public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.userControl21.GetText += new Action<string>(userControl21_GetText); // register gettext event.
this.userControl11.BtnClick += new EventHandler(userControl11_Click); // Register Click event.
}
string text = null;
void userControl11_Click(object sender, EventArgs e) // implement it
{
MessageBox.Show(text);
} void userControl21_GetText(string obj) // implement it.
{
text = obj;
}
}

2 个UserControl 的传值问题的更多相关文章

  1. C#中,用户控件UserControl里面用Panl加载UserControl,并实现利用委托互相传值

    用户控件主窗体结构:左侧树形菜单,右侧Panl: 根据点击的菜单节点,panl里面选择性加载某一个子窗体用户控件,并传值给子窗体: 反之,在子窗体进行相应的操作之后,传值给主窗体,触发主窗体的刷新. ...

  2. 一种开发模式:ajax + ashx + UserControl

    一.ajax+ashx模式的缺点     在web开发过程中,为了提高网站的用户体验,或多或少都会用到ajax技术,甚至有的网站全部采用ajax来实现,大量使用ajax在增强用户体验的同时会带来一些负 ...

  3. silverlight学习之页面传值篇

    1.silverlight 实现页面导航跳转   (1)利用根视图    A.修改App.xmal.cs     //使用根视图实现页面导航跳转        //申明一个Grid对象         ...

  4. mono for android Listview 里面按钮 view Button click 注册方法 并且传值给其他Activity 主要是context

    需求:为Listview的Item里面的按钮Button添加一个事件,单击按钮时通过事件传值并跳转到新的页面. 环境:mono 效果: 布局代码 主布局 <?xml version=" ...

  5. ASP.NET MVC 5 Web编程5 -- 页面传值的方式

    本篇文章将讲述MVC的页面传值方式,具体包括:后端向前端传值(Controller向View传值):前端向后端传值(View向Controller传值):Action与Action之间的传值. 回顾 ...

  6. MUI APP关于页面之间的传值,plusready和自定义事件

    最近在用MUI开发这个APP,发现有时候这个plusready不起作用,表现在,这个页面如果重复打开,这个plusready就进不去,然后上一个页面传过来的值,就没法接收了.这个经过MUI官方确认,是 ...

  7. Android开发之Activity的创建跳转及传值

    在Android系统的江湖中有四大组件:活动(Activity), 服务(Service), 广播接收器(Broadcast Reciver)和内容提供者(Content Provider).今天所介 ...

  8. MVC 传值

    1.ViewBag    Controller:ViewBag.Message = "Hello, Word";    View:@ViewBag.Message   注:View ...

  9. Java传值和传址

    调用函数时,传的参数过去可能是传值,也可能是传址.如果是传值,函数内部的操作对参数的值没有影响:如果是传址,函数内部的操作是对参数指向的内存进行操作,会影响参数的值. Java到底是传值还是传址?用下 ...

随机推荐

  1. 微软Dynamics 使用葡萄城的Wijmo 5提供移动端用户界面

    近日,全球最大的控件提供商葡萄城公司宣布: 葡萄城近日与微软公司达成合作,将Wijmo 产品线的HTML5和JaveScript 控件融合到微软Dynamics CRMOnline 2016版中. 随 ...

  2. paip.c3p0 nullpointexcept 配置文件根路径读取bug 解决

    paip.c3p0 nullpointexcept 配置文件根路径读取bug 解决 windows ok linux犯错误... 查看loging, 初始化的时候儿jdbcurl,user,pwd都是 ...

  3. 分享几段JavaScript

    正则表达式 /*http://www.cnblogs.com/woxpp/*/ intege: "^-?[1-9]\\d*$", /*整数*/ intege1: "^[1 ...

  4. 看2015年TFC游戏大会,云计算何以唱主角

    日前,第10界TFC游戏大会浩浩荡荡地在北京国际会议中心成功举办了.与往届不同的是,这一次TFC的金苹果奖被四家云计算公司夺走,它们分别是金山云.阿里云.腾讯云和首都在线.四家云计算公司夺走了游戏大会 ...

  5. 64-bit Tips

    终究还是来了.Apple下发了支持64位的最后通牒: As we announced in October, beginning February 1, 2015 new iOS apps submi ...

  6. 运用JMX监控Tomcat/Java jvisualvm简要说明

    公司线上项目出现了java heap space 然后经过查询知晓了下面工具于是开始了尝试. visualvm能干什么:监控内存泄露,跟踪垃圾回收,执行时内存.cpu分析,线程分析... jvisua ...

  7. apache url rewrite 的RewriteRule参数详解

    apache url rewrite 的RewriteRule参数详解 我们经常会在apache那边对一些url进行一些重写操作,那么就会涉及到很多的重写策略的选择,比如重定向的方式,参数的处理,规则 ...

  8. HtmlDocument

    HtmlDocument HtmlDocument类对应着一个HTML文档代码.它提供了创建文档,装载文档,修改文档等等一系列功能,来看看它提供的功能. 一.属性 int CheckSum { get ...

  9. Android BitmapShader 实战 实现圆形、圆角图片

    转载自:http://blog.csdn.net/lmj623565791/article/details/41967509 1.概述 记得初学那会写过一篇博客Android 完美实现图片圆角和圆形( ...

  10. navigationBar设置透明

    //设置全透明方式 一.完全不用图片(iOS7之后有效)[self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBar ...