c#中怎么样在用户控件中的一个事件按钮中得到父窗口的一个事件的触发?? 你可以把这个功能抽象成类,然后编译成dll文件,然后直接引用dll文件,然后在你以后要用到这功能的时候实例化后就可以用了 wish u good luck Greatsft 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我的想法好像跟楼主倒过来了。用户控件代码: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); } }} step1:在项目下声明一个代理://要传的数据类public class heheEventArgs:EventArgs{//要传的string或者放个对象也可以! public string xx;}//传输代理;public delegate void heheHandler(object sender, heheEventArgs e);step2:在RequestPurchaseBillListUI控件中添加一个public event heheHandler heheevent;在RequestPurchaseBillListUI控件Load事件中加入:{heheEventArgs arg = new heheEventArgs();arg.xx = "okle";this.heheevent(this,arg);}step3:在父窗口实例化RequestPurchaseBillListUI控件时,加入下面代码:RequestPurchaseBillListUI requestPurchaseBillListUI = new RequestPurchaseBillListUI();requestPurchaseBillListUI.heheevent += new heheHandler(this.hehe); (如果你的控件是托过去的,在父窗体的#region Windows 窗体设计器生成的代码里的实例化后加requestPurchaseBillListUI.heheevent += new heheHandler(this.Button_click);)同时在父控件里添加方法:private void hehe(object sender, heheEventArgs e){ MessageBox.Show(e.xx); this.Button_click(this,new System.EventArgs());}这个原理和VC的消息类似,是子控件发一个Message给父控件,父控件收到Message后调用对应的方法,这个方法再调用button_click方法。这是C#代码的运行机制,一定要会的,就和VC的消息机制一样重要! 用正则找出网页中某id是否含有指定字符串? c#如何将两个string类型的字符串连到一块儿 C#怎么调用PB的DLL文件 C# 反射调用类中的方法(在类未实例化前) 谁有好用的asp.net(C# 2.0)日历控件 请教Session的问题 如何将一张图片变为黑白? 请问在局域网中WEB SERVICE如何发布 求教关于删除提示框添加的问题 求帮忙把这一段c语言版的crc算法改为c#版,谢谢~ 求书《c#技术内幕》源代码 CS转SmartClient可行性
用户控件代码:
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);
} }
}
step1:
在项目下声明一个代理:
//要传的数据类
public class heheEventArgs:EventArgs
{
//要传的string或者放个对象也可以!
public string xx;
}
//传输代理;
public delegate void heheHandler(object sender, heheEventArgs e);step2:在RequestPurchaseBillListUI控件中添加一个
public event heheHandler heheevent;
在RequestPurchaseBillListUI控件Load事件中加入:
{
heheEventArgs arg = new heheEventArgs();
arg.xx = "okle";
this.heheevent(this,arg);
}step3:
在父窗口实例化RequestPurchaseBillListUI控件时,加入下面代码:
RequestPurchaseBillListUI requestPurchaseBillListUI = new RequestPurchaseBillListUI();
requestPurchaseBillListUI.heheevent += new heheHandler(this.hehe);
(如果你的控件是托过去的,在父窗体的#region Windows 窗体设计器生成的代码里的实例化后加requestPurchaseBillListUI.heheevent += new heheHandler(this.Button_click);)
同时在父控件里添加方法:
private void hehe(object sender, heheEventArgs e)
{
MessageBox.Show(e.xx);
this.Button_click(this,new System.EventArgs());
}这个原理和VC的消息类似,是子控件发一个Message给父控件,父控件收到Message后调用
对应的方法,这个方法再调用button_click方法。这是C#代码的运行机制,一定要会的,就和VC的消息机制一样重要!