在.net应用程序中使用用户控件
点击次数:15 次 发布日期:2008-11-26 23:44:43 作者:源代码网
|
源代码网推荐 源代码网推荐郑佐2004-11-30 源代码网推荐 源代码网推荐 做过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显示当前的文本框内容。 源代码网推荐 源代码网推荐控件显示如下: 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐在窗体中添加上面的用户控件,当我们改变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); 源代码网推荐 源代码网推荐} 源代码网推荐 源代码网推荐另外从VS.net中的工具箱中拖动用户控件到窗体上,如果是第一次需要编译一下项目。 源代码网推荐 源代码网推荐 源代码网推荐 源代码网供稿. |
