问题描述:有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. [iOS]The app icon set named "AppIcon" did not have any applicable content.

    Develop Tools: xCode 5.1 I write a demo for app settings feature. The tutorial url is here. When I a ...

  2. 解决TCP网络传输粘包问题

    很久之前就想写一写关于TCP粘包处理的文章了,无奈一直做WEB开发 没时间研究那个,拖了很久,最近要为一个客户做winform 服务器端,要用到SOCKET就发现了这个问题,这才想起来要解决. 下面用 ...

  3. [Python爬虫] scrapy爬虫系列 <一>.安装及入门介绍

    前面介绍了很多Selenium基于自动测试的Python爬虫程序,主要利用它的xpath语句,通过分析网页DOM树结构进行爬取内容,同时可以结合Phantomjs模拟浏览器进行鼠标或键盘操作.但是,更 ...

  4. 使用socket.io开发简单群聊功能

    1.新建package.json文件: { "name": "socket-chat-example", "version": " ...

  5. javaweb学习总结(十六)——JSP指令

    一.JSP指令简介 JSP指令(directive)是为JSP引擎而设计的,它们并不直接产生任何可见输出,而只是告诉引擎如何处理JSP页面中的其余部分. 在JSP 2.0规范中共定义了三个指令: pa ...

  6. Spring之@Configuration配置解析

    1.简单的示例: @Configuration @EnableConfigurationProperties({DemoProperties.class}) public class DemoConf ...

  7. 结构体快排回顾(sort)

    一般来说,我做竞赛的时候排序一般用快排 很快很方便 普通sort(从小到大) sort(a,a+n); 直接贴一段代码吧,包含了vector,sort,结构体等简单东西综合 #include < ...

  8. android: 从相册中选择照片

    虽然调用摄像头拍照既方便又快捷,但并不是每一次我们都需要去当场拍一张照片的. 因为每个人的手机相册里应该都会存有许许多多张照片,直接从相册里选取一张现有的照 片会比打开相机拍一张照片更加常用.一个优秀 ...

  9. MyEclipse xml 手动添加 dtd

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "- ...

  10. Express ejs 3.* layout.ejs

    新版本改成了 <%- include file.ejs %> 具体使用方法如下:1. views文件夹 下新建header.ejs,插入代码 <html><head> ...