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到底是传值还是传址?用下 ...
随机推荐
- [游戏模版16] Win32 飞机射击 敌人追踪
>_<:AI introduction. >_<:According the plane position (nowX,nowY) relative to birds' pos ...
- jQuery的XX如何实现?——4.类型检查
往期回顾: jQuery的XX如何实现?——1.框架 jQuery的XX如何实现?——2.show与链式调用 jQuery的XX如何实现?——3.data与cache机制 -------------- ...
- ComponentOne Xuni助力Xamarin开发者突破百万,快速开发Android、IOS Apps
在微软Build 2015上,随着VS 2015的预览版发布,Xamrine免费版已经作为VS 2015跨平台移动解决方案的核心.与此同时,Xamarin官方也宣布其用户量达到百万之多.2011年7月 ...
- EXCEL VBA入门篇之代码应用基础
- iOS开发之静态库(四)—— 静态框架framework制作
前面介绍过,虽然苹果公司禁止在iOS开发中使用动态库,而且也从XCode中移除了创建静态框架的功能,但我们还是可以通过对XCode进行小小的改动来创建静态框架. 编译环境:Macbook Air + ...
- Leetcode 165 Compare Version Numbers
题意:比较版本号的大小 有点变态,容易犯错 本质是字符串的比较,请注意他的版本号的小数点不知1个,有的会出现01.0.01这样的变态版本号 class Solution { public: int c ...
- SSE技术详解:一种全新的HTML5服务器推送事件技术
前言 一般来说,Web端即时通讯技术因受限于浏览器的设计限制,一直以来实现起来并不容易,主流的Web端即时通讯方案大致有4种:传统Ajax短轮询.Comet技术.WebSocket技术.SSE(Ser ...
- 定义一个时钟类——Clock,它包括三个int型 成员变量分别表示时、分、秒,一个构造方法用于对三个成员变量(时、分、秒) 进行初始化,还有一个成员方法show()用于显示时钟对象的时间。其次,再定义 一个主类——TestClass,在主类的main方法中创建多个时钟类的对象,使用这 些对象调用方法show()来显示时钟的时间
package java1; public class Clock { int hhh; int mmm; int sss; Clock(int h,int m,int s) { hhh=h; mmm ...
- linux开发神器--Tmux
在Linux上工作的朋友,可能会常常遇到这种情况: 痛点1: 在公司用putty连着Linux开发机,程序正跑着,不知不觉到了下班的时候,要和老婆/女朋友约会了,但是程序还没跑完,并且进度有点紧,还得 ...
- HTML5 <a>标签的ping属性用法
随着移动互联网用户井喷式的增长,web前端开发中的HTML5在近几年备受瞩目,越来越多的人从事html5开发相关工作.今天小编也来凑个热闹,和大家一起来谈谈HTML5中<a>标签的ping ...