2 个UserControl 的传值问题
问题描述:有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 的传值问题的更多相关文章
- C#中,用户控件UserControl里面用Panl加载UserControl,并实现利用委托互相传值
用户控件主窗体结构:左侧树形菜单,右侧Panl: 根据点击的菜单节点,panl里面选择性加载某一个子窗体用户控件,并传值给子窗体: 反之,在子窗体进行相应的操作之后,传值给主窗体,触发主窗体的刷新. ...
- 一种开发模式:ajax + ashx + UserControl
一.ajax+ashx模式的缺点 在web开发过程中,为了提高网站的用户体验,或多或少都会用到ajax技术,甚至有的网站全部采用ajax来实现,大量使用ajax在增强用户体验的同时会带来一些负 ...
- silverlight学习之页面传值篇
1.silverlight 实现页面导航跳转 (1)利用根视图 A.修改App.xmal.cs //使用根视图实现页面导航跳转 //申明一个Grid对象 ...
- mono for android Listview 里面按钮 view Button click 注册方法 并且传值给其他Activity 主要是context
需求:为Listview的Item里面的按钮Button添加一个事件,单击按钮时通过事件传值并跳转到新的页面. 环境:mono 效果: 布局代码 主布局 <?xml version=" ...
- ASP.NET MVC 5 Web编程5 -- 页面传值的方式
本篇文章将讲述MVC的页面传值方式,具体包括:后端向前端传值(Controller向View传值):前端向后端传值(View向Controller传值):Action与Action之间的传值. 回顾 ...
- MUI APP关于页面之间的传值,plusready和自定义事件
最近在用MUI开发这个APP,发现有时候这个plusready不起作用,表现在,这个页面如果重复打开,这个plusready就进不去,然后上一个页面传过来的值,就没法接收了.这个经过MUI官方确认,是 ...
- Android开发之Activity的创建跳转及传值
在Android系统的江湖中有四大组件:活动(Activity), 服务(Service), 广播接收器(Broadcast Reciver)和内容提供者(Content Provider).今天所介 ...
- MVC 传值
1.ViewBag Controller:ViewBag.Message = "Hello, Word"; View:@ViewBag.Message 注:View ...
- Java传值和传址
调用函数时,传的参数过去可能是传值,也可能是传址.如果是传值,函数内部的操作对参数的值没有影响:如果是传址,函数内部的操作是对参数指向的内存进行操作,会影响参数的值. Java到底是传值还是传址?用下 ...
随机推荐
- [JS12] 统计访问次数
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- JavaScript自定义事件
很多DOM对象都有原生的事件支持,向div就有click.mouseover等事件,事件机制可以为类的设计带来很大的灵活性,相信.net程序员深有体会.随着web技术发展,使用JavaScript自定 ...
- 无线客户端框架设计(5.1):将JSON映射为实体对象(iOS篇)
iOS开发人员已经习惯于将JSON转换为字典或者数组来进行操作了,接下来我要做的事情,可能匪夷所思,但是,对WP和Android开发人员而言,他们更倾向于将JSON转换为实体对象进行操作. 我所设计的 ...
- Fiddler 修改返回内容 OnBeforeResponse 无效 没用
Fiddler自定义脚本可以实现很强大的内容替换,包括很有意义的——修改返回内容. 具体的方法可以参考官网:http://docs.telerik.com/fiddler/KnowledgeBase/ ...
- javaWEB项目感受
1.WEB-INF下的内容是受保护的,不能直接访问,可以通过转发的方式访问. 2.OGNL技术: 对象图像导航语言,是一种功能强大的表达式语言.可以让我们用非常简单的表达式访问对象层. OGNL引擎访 ...
- [ZT]Language codes – MFC
Below is table with all MFC language codes. I think it can be sometimes very useful. First column c ...
- eclipse、myeclipse,svn插件subclipse 忘记密码的解决方法(win7、win8、xp)
如果是Windows7.Windows8系统只要删除当前用户目录下的AppData\Roaming\Subversion\auth\svn.simple 比如我的用户名taoweiji,就删除C:\U ...
- C# 汉字转拼音 使用微软的Visual Studio International Pack 类库提取汉字拼音首字母
代码参考该文http://www.cnblogs.com/yazdao/archive/2011/06/04/2072488.html VS2015版本 1.使用Nuget 安装 "Simp ...
- 【Vegas原创】RHEL6.2安装vmtools
1,mount虚拟cdrom # mount /dev/cdrom /mnt/cdrom 2, ls一下,看有没有 # cd /mnt/cdrom # ls –l ...
- 一个类有两个方法,其中一个是同步的,另一个是非同步的; 现在又两个线程A和B,请问:当线程A访问此类的同步方法时,线程B是否能访问此类的非同步方法?
一个类有两个方法,其中一个是同步的,另一个是非同步的:现在又两个线程A和B,请问:当线程A访问此类的同步方法时,线程B是否能访问此类的非同步方法? 答案:可以 验证 package com.my.te ...