郑佐<chsdate w:st="on" isrocdate="False" islunardate="False" day="30" month="11" year="2004"><span lang="EN-US" style="FONT-SIZE: 9pt">2004-11-30</span></chsdate>
做过asp.net的人都知道开发的时候使用用户控件很方便,为功能模块化提供了相当大的灵活性。令人高兴的是开发Windows窗体也可以使用用户控件。这里我们来看看为用户控件添加属性和事件,并实现把消息发送到父容器。本文主要是为没有使用过用户控件的朋友提供一些参考。
用户控件的实现比较简单,直接从System.Windows.Forms.UserControl。
public class UserControl1 : System.Windows.Forms.UserControl
为了便于测试我在上面添加了一个TextBox,并注册TextBox的TextChanged事件,
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
事件处理函数,
private void textBox1_TextChanged(object sender, System.EventArgs e)
{
MessageBox.Show(this.textBox1.Text);
}
这里演示如果控件中文本框的内容改变就会用MessageBox显示当前的文本框内容。
控件显示如下:
<shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><lock aspectratio="t" v:ext="edit"></lock></shapetype>
在窗体中添加上面的用户控件,当我们改变textBox的文本时,可以看到跳出一个对话框,很简单吧。
下面来看看对控件添加属性。
这里定义一个私有变量。
private string customValue;
添加访问他的属性
public string CustomValue
{
get{return customValue;}
set{customValue =value;}
}
在窗体中使用的时候像普通控件一样进行访问,
userControl11.CustomValue = "用户控件自定义数据";
通过事件可以传递消息到窗体上,在定义之前我们先来写一个简单的参数类。
public class TextChangeEventArgs : EventArgs
{
private string message;
public TextChangeEventArgs(string message)
{
this.message = message;
}
public string Message
{
get{return message;}
}
}
定义委托为,
public delegate void TextBoxChangedHandle(object sender,TextChangeEventArgs e);
接下去在用户控件中添加事件,
//定义事件
public event TextBoxChangedHandle UserControlValueChanged;
为了激发用户控件的新增事件,修改了一下代码,
private void textBox1_TextChanged(object sender, System.EventArgs e)
{
if(UserControlValueChanged != null)
UserControlValueChanged(this,new TextChangeEventArgs(this.textBox1.Text));
}
好了,为了便于在Csdn上回答问题,把完整的代码贴了出来:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace ZZ.WindowsApplication1
{
public class UserControl1 : System.Windows.Forms.UserControl
{
private System.Windows.Forms.TextBox textBox1;
private string customValue;
private System.ComponentModel.Container components = null;
public string CustomValue
{
get{return customValue;}
set{customValue =value;}
}
//定义事件
public event TextBoxChangedHandle UserControlValueChanged;
public UserControl1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region 组件设计器生成的代码
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
this.textBox1.Location = new System.Drawing.Point(12, 36);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
this.Controls.Add(this.textBox1);
this.Name = "UserControl1";
this.Size = new System.Drawing.Size(150, 92);
this.ResumeLayout(false);
}
#endregion
private void textBox1_TextChanged(object sender, System.EventArgs e)
{
if(UserControlValueChanged != null)
UserControlValueChanged(this,new TextChangeEventArgs(this.textBox1.Text));
}
}
//定义委托
public delegate void TextBoxChangedHandle(object sender,TextChangeEventArgs e);
public class TextChangeEventArgs : EventArgs
{
private string message;
public TextChangeEventArgs(string message)
{
this.message = message;
}
public string Message
{
get{return message;}
}
}
}
使用时要在窗体中注册上面的事件,比较简单都贴源代码了,
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace ZZ.WindowsApplication1
{
public class Form1 : System.Windows.Forms.Form
{
private WindowsApplication1.UserControl1 userControl11;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
userControl11.CustomValue = "用户控件自定义数据";
userControl11.UserControlValueChanged += new TextBoxChangedHandle(userControl11_UserControlValueChanged);
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
this.userControl11 = new WindowsApplication1.UserControl1();
this.SuspendLayout();
this.userControl11.Location = new System.Drawing.Point(8, 8);
this.userControl11.Name = "userControl11";
this.userControl11.Size = new System.Drawing.Size(150, 84);
this.userControl11.TabIndex = 0;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 193);
this.Controls.Add(this.userControl11);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void <place w:st="on">Main</place>()
{
Application.Run(new Form1());
}
private void userControl11_UserControlValueChanged(object sender, TextChangeEventArgs e)
{
MessageBox.Show("当前控件的值为:" + e.Message);
}
}
}
另外需要动态加载,就把控件添加在容器的Controls集合就行了,下面是在构造函数中添加控件,
public Form1()
{
InitializeComponent();
UserControl1 uc = new UserControl1();
uc.CustomValue = "动态加载的用户控件";
uc.UserControlValueChanged += new TextBoxChangedHandle(userControl11_UserControlValueChanged);
this.Controls.Add(uc);
}
另外从VS.net中的工具箱中拖动用户控件到窗体上,如果是第一次需要编译一下项目。
分享到:
相关推荐
1、编写一个Web应用程序Login,该程序用于检查用户登录信息。当用户输入正确的用户名和密码时,显示登录成功的消息。否则,显示登录不正确的消息。 2、完成如下选择城市的程序,要求单击左向箭头时,把右边ListBox中...
很简单的换皮肤控件,内有使用方法,美化你的应用程序
1、本程序使用基于Asp.Net的6种Asp验证控件; 2、验证控件十分方便,可以帮助我们很好解决提示用户输入的作用,达到一种很好的用户体验效果。 3、本程序,采用了非空验证(RequireValidator)、范围验证...
asp.net 在应用程序之间进行拖放操作源码! 很值得下载看看!资源免费,大家分享!!
8.3 ASP.NET应用程序中的用户状态管理 8.3.1 Global.asax文件概述 8.3.2 Application对象事件 8.3.3 Session对象事件 8.3.4 其他几种客户端的用户状态管理方法 8.3.5 本小节提示 8.4 ASP....
第2章:在进行ASP.NET应用程序开发前,首先需要了解ASP.NET应用程序开发的最主要的编程语言C#,由于ASP.NET应用程序是基于面向对象的思想的,所以C#编程语言也包括多种面向对象的特性,包括多态和继承等,本章讲解了...
8.3ASP.NET应用程序中的用户状态管理 8.3.1Global.asax文件概述 8.3.2Application对象事件 8.3.3Session对象事件 8.3.4其他几种客户端的用户状态管理方法 8.3.5本小节提示 8.4ASP.NETHTFP运行情况 8.4.1HTYP运行‘晴...
我们知道,TextBox控件允许用户把数据输入到应用程序中,但是自身并没有提供任何类型的校验和输入过滤功能。为此,你需要对每一个TextBox控件编写自己的定制校验和过滤逻 辑。显然,当需要在工程中大量地应用这种...
全书主要介绍了ASP.NET应用程序基础、C#语言、Web页面设计基础、Web控件、ASP.NET内置对象等,在ASP.NET中使用XML,以SQLServer2005为基础进行ADO.NET数据库编程,使用用户控件、自定义控件,创建Web服务。...
9.3 在SqlDataSource控件中使用ASP.NET参数 271 9.3.1 使用ASP.NET参数对象 272 9.3.2 使用ASP.NET的Control-Parameter对象 274 9.3.3 使用ASP.NET的Cookie-Parameter对象 277 9.3.4 使用ASP.NET的Form-Parameter...
本程序应用技术:1、SQL自定义函数,减少每次使用连接过程语句, 2、使用dataset缓存表管理datagridview控件的视图 3、使用sql查询数据库列表、绑定数据源到ComBobox列表框控件,提供选项 4、datagridview控件某...
这是一个串口控件,大家知道.net自带的串口类不适合做以数据包为通信单位的应用程序。而嵌入式开发过程却很需要这个特性;.net自带控件也太死板,不能通过API来修改串口的超时参数,而这个控件却可以,代码编写效率...
第4章 在Windows窗体应用程序中使用数据 第5章 与托管对象进行互操作 第6章 Windows窗体应用程序中的报表和打印 第7章 异步编程 第8章 增强应用程序的可用性 第9章 部署Windows窗体应用程序 第10章 Windows窗体...
运用ASP.NET开发Web应用程序过程中,DataGrid是一个非常重要的控件,几乎任何和数据相关的表现都要用到该控件。所以熟练掌握DataGrid控件的应用技巧是每个Web开发人员所必备的基本能力。
第5章 在 Microsoft ASP.NET Web 窗体中添加代码 第6章 Microsoft ASP.NET Web 应用程序的跟踪机制 第7章 验证用户输入 第8章 创建用户控件 第9章 使用 Microsoft Visual Studio .NET 访问关系型数据 第10章 使用 ...
本书全面介绍ASP.NET的基础知识和网络应用程序设计方法。全书由9章组成:内容包括ASP.NET运行及编程环境、HTML语言、VB.NET语言基础、VB.NET面向对象程序设计、ASP.NET基本语法与常用内置对象、Web窗体及服务器控件...
全书论述了ASP.NET开发概述、C#及ADO.NET背景知识、ASP.NET Web窗体的基本控件、数据控件和数据绑定技术、用户控件和自定义控件、ASP.NET内置对象和缓存技术、ASP.NET应用程序配置及编译和部署、ASP.NET与Web服务、...
VB.NET中Winsock控件的使用详解 axWinsock vb.net
Asp.Net中使用水晶报表 javascript提示类 分页 文件操作 ASP.NET 2.0高级控件之FileUpload控件 如何在ASP.NET页面间传送数据 等