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到底是传值还是传址?用下 ...
随机推荐
- [JS4] 最简单JS框架
<html> <head> <title></title> <SCRIPT TYPE="text/JavaScript"> ...
- [JS11] 状态栏滚动
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- Hive 安装配置
系统:Ubuntu 64 bit系统 step1:创建hive文件目录,并在hive目录下创建tmp,warehouse 和 log目录: Step2:解压hive安装包: Step3:创建配置文件: ...
- atitit.条形码的原理与生成总结java Barcode4j barcode o5
atitit.条形码的原理与生成总结java Barcode4j barcode o5 条形码类库使用报告Barcode4j, ZXing 1 使用成果图片 1 条形码标准code 128和code ...
- paip.gui控件form窗体的原理实现以及easyui的新建以及编辑实现
paip.gui控件form窗体的原理实现以及easyui的新建以及编辑实现 //////新建 与编辑 var EditForm=new Form_easyui(); if(row) ...
- [转]Oracle因安装时未设定字符集导致中文乱码的解决方案
在CentOS 6.4上安装Oracle 11g没有设定字符集,采用的是操作系统默认字符集:WE8MSWIN1252,将字符集修改为:AL32UTF8. SQL> select userenv( ...
- Leetcode 206 Reverse Linked List 链表
将单向链表反转 完成如图操作,依次进行即可 1 2 3 /** * Definition for singly-linked list. * struct ListNode { * int val; ...
- 用自己的算法实现startsWith和endsWith功能
package hanqi; import java.util.Random; import java.util.Scanner; public class zuoye { public static ...
- 微信开发——OAuth2.0授权
微信公众平台最近新推出微信认证,认证后可以获得高级接口权限,其中一个是OAuth2.0网页授权,很多朋友在使用这个的时候失败了或者无法理解其内容,希望我出个教程详细讲解一下,于是便有了这篇文章. 一. ...
- FGPA 双向 IO 自动方向控制
Using a Virtex Device to Drive 5V CMOS-Level Signals Voltage Level-Shifter Output Waveform