在.net应用程序中使用用户控件
2011-06-23 11:12
435 查看
http://blog.csdn.net/zhzuo/archive/2004/11/30/199599.aspx
http://topic.csdn.net/t/20050220/20/3793694.html
做过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显示当前的文本框内容。
控件显示如下:
![](http://blog.csdn.net/images/blog_csdn_net/zhzuo/18609/o_winuc.gif)
在窗体中添加上面的用户控件,当我们改变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 Main()
{
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);
//uc.Location=new System.Drawing.Point(3,3); //修改显示位置
uc.Visible=true; //是否显示
}
另外从VS.net中的工具箱中拖动用户控件到窗体上,如果是第一次需要编译一下项目。
http://topic.csdn.net/t/20050220/20/3793694.html
做过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显示当前的文本框内容。
控件显示如下:
![](http://blog.csdn.net/images/blog_csdn_net/zhzuo/18609/o_winuc.gif)
在窗体中添加上面的用户控件,当我们改变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 Main()
{
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);
//uc.Location=new System.Drawing.Point(3,3); //修改显示位置
uc.Visible=true; //是否显示
}
另外从VS.net中的工具箱中拖动用户控件到窗体上,如果是第一次需要编译一下项目。
相关文章推荐
- 在.net应用程序中使用用户控件 [zt from zz]
- 在.net应用程序中使用用户控件(转)
- 在.net应用程序中使用用户控件
- 在.net应用程序中使用用户控件
- 在.net应用程序中使用用户控件
- 在.net应用程序中使用用户控件-.NET教程,组件控件开发(转载)
- 在.net窗体程序中使用用户控件
- .net 简单图表控件 (介绍测试示例使用部分) [b/s应用程序控件] I
- 如何在aspx网页中使用.net编译后的自定义用户控件
- .net 简单图表控件 (介绍测试示例使用部分) [c/s桌面应用程序控件] II
- .netWeb用户控件使用技巧。
- .NET----在用户控件利用包装属性+反射,使用引用页的ViewState
- 如何使用Javascript调用.NET 2.0用户控件中的方法和属性
- 可以十倍地提高.NET 应用程序的速度集群存储器对象缓存控件NCache
- 未能创建视频预览。请检查设备连接,确定没有其他应用程序或用户使用该设备
- 对于目前用户使用PTAEXT.dll控件引起浏览器崩溃问题!
- 使用.NET开发数据库应用程序(1)---给初学者看的文章
- C#下使用Winform用户控件,Host vtkRenderWindow,来实现vtk与winform集成
- C#通过多线程为基于 .NET 的应用程序实现响应迅速的用户
- ZZ:使用.NET语言开发Silverlight应用程序入门(二):界面和对象模型