你想将其中的button的click事件作为你的用户空间的click事件,公开?
解决方案 »
- C# 中继承窗体的paint事件为何在窗体加载时不执行?
- <asp:literal id="ErrorMsg" runat="server"></asp:literal>这是真么意思啊
- C# 开发64位与32位通用的项目
- 谁有c#入门经典(第四版)电子书下载?
- datagridview 一些问题 小弟恭候多时了,欢迎高手拜访
- 急,将pdf文件转成word
- 用VB做的WFM程序,为什么输入法会自动转换到全角啊???
- 请教:静态页面和动态页面的流量问题。
- TcpListener问题(100分求助).用一个线程启动TcpListener后,该线程不能被结束。
- 异步通讯
- 无法找到组件?
- 发一个分页的存储过程,请大家看看那些地方应该再改一改..
tr.MouseDown+=new System.Windows.Forms.MouseEventHandler(this.trMouseDown);
public void trMouseDown(object sender ,System.Windows.Forms.MouseEventArgs e)
{
((TreeView)sender).SelectedNode=((TreeView)sender).GetNodeAt(e.X,e.Y);
}
public event EventHandler TextBoxClick;//这里是自定义TextBox的Click事件
//...
this.TextBox.Click += new EventHandler(TextBox_Click);//这里订阅事件
//...
private void TextBox_Click(object sender,System.EventArgs e)//这里处理事件
{
if (TextBoxClick!= null)
{
TextBoxClick(sender,e);//引发事件
}
}
//*在外部调用时
UserControl.TextBoxClick += new EventHandler(TextBoxClick);//处理事件
private void TextBoxClick(object sender,System.EventArgs e)//这里处理事件
{
//...
}
[Category("Action")]
public event EventHandler Click;
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;namespace ZZTestForm
{
///定义事件代理
public delegate void TextBoxChangedHandle(string message);
/// <summary>
/// UserControl1 的摘要说明。
/// </summary>
public class UserControl1 : System.Windows.Forms.UserControl
{
///定义事件
public event TextBoxChangedHandle TextBoxChanged;
private System.Windows.Forms.TextBox textBox1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public UserControl1()
{
InitializeComponent();
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(20, 32);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
//
// UserControl1
//
this.Controls.Add(this.textBox1);
this.Name = "UserControl1";
this.Size = new System.Drawing.Size(150, 96);
this.ResumeLayout(false); }
#endregion private void textBox1_TextChanged(object sender, System.EventArgs e)
{
TextBoxChanged(this.textBox1.Text);
}
}
}
主窗体代码:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace ZZTestForm
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private ZZTestForm.UserControl1 userControl11;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
InitializeComponent();
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.userControl11 = new ZZTestForm.UserControl1();
this.SuspendLayout();
//
// userControl11
//
this.userControl11.Location = new System.Drawing.Point(96, 48);
this.userControl11.Name = "userControl11";
this.userControl11.Size = new System.Drawing.Size(150, 96);
this.userControl11.TabIndex = 0;
this.userControl11.TextBoxChanged += new ZZTestForm.TextBoxChangedHandle(this.userControl11_TextBoxChanged);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(332, 217);
this.Controls.Add(this.userControl11);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void userControl11_TextBoxChanged(string message)
{
MessageBox.Show("你改变了用户控件中TextBox的值:"+message);
} }
}
但略有不足的是无法在空间中直接双击引发该事件的处理(像Button那样)
如果有解决办法的话请给我留言
谢